My assignment instructions are "rolling" 2 dice and getting the sum and then finding the probability that that sum came up based on how many times the user wants to roll the die. I must use a nested loop and I can't use separate loops for each dice combination (which I haven't done). I am not allowed to use arrays in this assignment.
Write a program to simulate tossing a pair of 11-sided dice and determine the percentage of times each possible combination of the dice is rolled.
Create a new project called 5.05 Random Dice in the Mod05 Assignments folder.
Create a class called DiceProbability in the newly created project folder.
Ask the user to input how many times the dice will be rolled.
Calculate the probability of each combination of dice. (You may want to start with more familiar six-sided dice.)
Print the results neatly in two columns
I need help in finding out what I put into the second "for" loop. Sorry for the messy list of integers and if statements. The code is unfinished.
import java.util.Random;
import java.util.Scanner;
public class DiceProbability
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Random randNum = new Random();
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;
int count8 = 0;
int count9 = 0;
int count10 = 0;
int count11= 0;
int count12= 0;
int count13 = 0;
int count14 = 0;
int count15 = 0;
int count16 = 0;
int count17 = 0;
int count18 = 0;
int count19 = 0;
int count20 = 0;
int count21 = 0;
int count22 = 0;
int die1 = 0, die2 = 0;
int rolls = 0;
int actualDiceSum;
double probabilityOfDice = 0.0;
System.out.print("Number of Rolls: ");
rolls = in.nextInt();
for(int timesRolled = 0; timesRolled < rolls; timesRolled++)
{
die1 = randNum.nextInt(12);
die2 = randNum.nextInt(12);
actualDiceSum = die1 + die2;
for()
{
if(actualDiceSum == 2){
count2++;
probabilityOfDice = count2 / rolls;
}
else if(actualDiceSum == 3){
count3++;
probabilityOfDice = count3 / rolls;
}
else if(actualDiceSum == 4){
count4++;
probabilityOfDice = count4 / rolls;
}
else if(actualDiceSum == 5){
count5++;
probabilityOfDice = count5 / rolls;
}
else if(actualDiceSum == 6){
count6++;
probabilityOfDice = count6 / rolls;
}
else if(actualDiceSum == 7){
count7++;
probabilityOfDice = count7 / rolls;
}
else if(actualDiceSum == 8){
count8++;
probabilityOfDice = count8 / rolls;
}
else if(actualDiceSum == 9){
count9++;
probabilityOfDice = count9 / rolls;
}
else if(actualDiceSum == 10){
count10++;
probabilityOfDice = count10 / rolls;
}
else if(actualDiceSum == 11){
count11++;
probabilityOfDice = count11 / rolls;
}
else if(actualDiceSum == 12){
count12++;
probabilityOfDice = count12 / rolls;
}
else if(actualDiceSum == 13){
count13++;
probabilityOfDice = count13 / rolls;
}
else if(actualDiceSum == 14){
count14++;
probabilityOfDice = count14 / rolls;
}
else if(actualDiceSum == 15){
count15++;
probabilityOfDice = count15 / rolls;
}
else if(actualDiceSum == 16){
count16++;
probabilityOfDice = count16 / rolls;
}
else if(actualDiceSum == 17){
count17++;
probabilityOfDice = count17 / rolls;
}
else if(actualDiceSum == 18){
count18++;
probabilityOfDice = count18 / rolls;
}
else if(actualDiceSum == 19){
count19++;
probabilityOfDice = count19 / rolls;
}
else if(actualDiceSum == 20){
count20++;
probabilityOfDice = count20 / rolls;
}
else if(actualDiceSum == 21){
count21++;
probabilityOfDice = count21 / rolls;
}
else if(actualDiceSum == 22){
count22++;
probabilityOfDice = count22 / rolls;
}
}
}
System.out.println("Sum of Dice \t\t Probability");
System.out.println("2's\t\t" + probabilityOfDice + "%");
System.out.println("3's\t\t" + probabilityOfDice + "%");
System.out.println("4's\t\t" + probabilityOfDice + "%");
System.out.println("5's\t\t" + probabilityOfDice + "%");
System.out.println("6's\t\t" + probabilityOfDice + "%");
System.out.println("7's\t\t" + probabilityOfDice + "%");
System.out.println("8's\t\t" + probabilityOfDice + "%");
System.out.println("9's\t\t" + probabilityOfDice + "%");
System.out.println("10's\t\t" + probabilityOfDice + "%");
System.out.println("11's\t\t" + probabilityOfDice + "%");
System.out.println("12's\t\t" + probabilityOfDice + "%");
System.out.println("13's\t\t" + probabilityOfDice + "%");
System.out.println("14's\t\t" + probabilityOfDice + "%");
System.out.println("15's\t\t" + probabilityOfDice + "%");
System.out.println("16's\t\t" + probabilityOfDice + "%");
System.out.println("17's\t\t" + probabilityOfDice + "%");
System.out.println("18's\t\t" + probabilityOfDice + "%");
System.out.println("19's\t\t" + probabilityOfDice + "%");
System.out.println("20's\t\t" + probabilityOfDice + "%");
System.out.println("21's\t\t" + probabilityOfDice + "%");
System.out.println("22's\t\t" + probabilityOfDice + "%");
}
}
Well according to me you don't require 2nd for loop.
Every time a dice is rolled you calculate the the sum and increase the count according to the sum.
you would require to use separate variables for calculating the probability of each sum.
For example
Probability of count2 = (count2/number of rolls);
Probability of count3 = (count2/number of rolls);
Use separate variables for probability of counts
try this code
import java.util.Random;
import java.util.Scanner;
public class DiceProbability
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Random randNum = new Random();
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;
int count8 = 0;
int count9 = 0;
int count10 = 0;
int count11= 0;
int count12= 0;
int count13 = 0;
int count14 = 0;
int count15 = 0;
int count16 = 0;
int count17 = 0;
int count18 = 0;
int count19 = 0;
int count20 = 0;
int count21 = 0;
int count22 = 0;
int die1 = 0, die2 = 0;
int rolls = 0;
int actualDiceSum;
double probabilityOfDice = 0.0;
System.out.print("Number of Rolls: ");
rolls = in.nextInt();
for(int timesRolled = 0; timesRolled < rolls; timesRolled++)
{
die1 = randNum.nextInt(12);
die2 = randNum.nextInt(12);
actualDiceSum = die1 + die2;
if(actualDiceSum == 2){
count2++;
}
else if(actualDiceSum == 3){
count3++;
}
else if(actualDiceSum == 4){
count4++;
}
else if(actualDiceSum == 5){
count5++;
}
else if(actualDiceSum == 6){
count6++;
}
else if(actualDiceSum == 7){
count7++;
}
else if(actualDiceSum == 8){
count8++;
}
else if(actualDiceSum == 9){
count9++;
}
else if(actualDiceSum == 10){
count10++;
}
else if(actualDiceSum == 11){
count11++;
}
else if(actualDiceSum == 12){
count12++;
}
else if(actualDiceSum == 13){
count13++;
}
else if(actualDiceSum == 14){
count14++;
}
else if(actualDiceSum == 15){
count15++;
}
else if(actualDiceSum == 16){
count16++;
}
else if(actualDiceSum == 17){
count17++;
}
else if(actualDiceSum == 18){
count18++;
}
else if(actualDiceSum == 19){
count19++;
}
else if(actualDiceSum == 20){
count20++;
}
else if(actualDiceSum == 21){
count21++;
}
else if(actualDiceSum == 22){
count22++;
}
}
}
System.out.println("Sum of Dice \t\t Probability");
System.out.println("2's\t\t" + count2/rolls + "%");
System.out.println("3's\t\t" + count3/rolls + "%");
System.out.println("4's\t\t" + count4/rolls + "%");
System.out.println("5's\t\t" + count5/rolls + "%");
//and so on...
}
}
I think you need to use two dimensional array as below:
int rolls = 0;
System.out.print("Number of Rolls: ");
rolls = in.nextInt();
int[][] numAndOccuranceCount= new int[11][2]; //sum will be between 2 -12
//initialize your array
for(int indx=0; indx<11;indx++){
numAndOccuranceCount[indx] = new int[]{indx+2,0);
}
for(int timesRolled = 0; timesRolled < rolls; timesRolled++){
die1 = randNum.nextInt(12);
die2 = randNum.nextInt(12);
actualDiceSum = die1 + die2;
for(int indx=0; indx<11;indx++){
if(actualDiceSum == numAndOccuranceCount[i][0]){
numAndOccuranceCount[indx][1] = numAndOccuranceCount[indx][1]+1;
break;
}
}
}
double proabability = 0.0;
//compute and print the probablity as below:
for(int indx=0; indx<11;indx++){
proabability = numAndOccuranceCount[indx][1]/rolls;
System.out.println("Probability of "+ numAndOccuranceCount[indx][0] +" = "+proabability);
}
I have some comment on your code.
Based on the instruction, you sum up the dice1 and dice2 is not a correct approach 'cause 1 + 5 = 2 + 4 = 3 + 3 but they are different. So we have to calculate the possibility based on the combination, not the sum.
Here's my code:
DTO class:
public class DiceCombination {
private int dice1;
private int dice2;
public DiceCombination(int dice1, int dice2) {
this.dice1 = dice1;
this.dice2 = dice2;
}
/* (non-Javadoc)
* #see java.lang.Object#toString()
*/
#Override
public String toString() {
return "Combination of [dice " + dice1 + " and dice " + dice2 + "]";
}
/* (non-Javadoc)
* #see java.lang.Object#hashCode()
*/
#Override
public int hashCode() {
final int prime = 31;
if(dice1 < dice2) {
return prime*dice1 + dice2;
}
else {
return prime*dice2 + dice1;
}
}
/* (non-Javadoc)
* #see java.lang.Object#equals(java.lang.Object)
*/
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DiceCombination other = (DiceCombination) obj;
if ((dice1 == other.dice1 && dice2 == other.dice2) || (dice1 == other.dice2 && dice2 == other.dice1))
return true;
return false;
}
}
Main class:
public class Posibility {
public static void main(String[] args) {
Map<DiceCombination,Integer> possibility = new HashMap<DiceCombination,Integer>();
int dice1;
int dice2;
int roll = 400; // value here should be inputted from user, you may change any value you want
Random randNum = new Random();
for(int i = 0; i < roll; i ++) {
dice1 = randNum.nextInt(12);
dice2 = randNum.nextInt(12);
DiceCombination dc = new DiceCombination(dice1, dice2);
if(possibility.containsKey(dc)) {
possibility.put(dc , possibility.get(dc) + 1);
}
else {
possibility.put(dc, 1);
}
}
for(DiceCombination key : possibility.keySet()) {
System.out.println("Result: " + key.toString() + " is " + ((double)possibility.get(key)/roll));
}
}
}
I agree with #Thinhbk that the question is technically worded in terms of combinations not sums but the picture in the link shows sums so I believe sums is the intention. The combinations approach as you have coded is impressive but too advanced for what appears to be an intro course to Java. I like how the picture shows that the IDE is BlueJ. Takes me back. Anyway, the assignment description doesn't specify the need for an inner for loop. I see #user1713336's edit that arrays are not allowed else before your (user1713336's) first loop you could add:
int[] count = new int[23];
//0 is the default value for each
//int[22] is the 23rd value in the array
What is your entire inner for loop could be replaced with:
count[actualDiceSum]++;
Then while printing in a for loop, print count[i]/rolls*100 and start int i at 2, the lowest possible value. I suspect you'll learn arrays next so I wanted to show how it'd be done.
Since you can't use arrays, your countx = 0; lines are fine as is. Don't calculate the probability in real time. It's unnecessarily and and adds many lines of code, just keep track of the number of times each value is rolled so:
if(actualDiceSum == 2)
count2++;
else if(actualDiceSum == 3)
count3++;
//etc
Note that you don't need to use {} if only one line of code is tied to the if or while statement as shown. Then print the same way as before, count2/rolls*100 (Multiply by 100 since the picture showed percentages, not ratios).
Related
I am making a dice game, the rules are 2 dice are thrown randomly, if the sum of both dice are 7 or 11 in the first try, the user wins. if the sum is 2,3 or 12 in the first try, the user loses. if the sum is 4,5,6,8,9,10 then that sum is set as an establish point and continues to throw the dice till the sum matches the established point and the user wins, however if the establish point is 7 the user loses.
the game plays 3 times and records how many times I have won or lost. however, I am not able to get that part finished, help would be appreciated, my code is bellow.
package roll;
import java.util.Random;
public class RandomSumGame {
boolean start;
int d1;
int d2;
int sum = d1 + d2;
int valuepoint;
String plus = "+";
public void play(int d1, int d2) {
int win = 0;
int loss = 0;
sum = d1 + d2;
for (;;) {
if (sum == 2 || sum == 3 || sum == 12) {
start = false;
loss++;
System.out.println(" = " + sum + ";you lose");
break;
} else if (sum == 7 || sum == 11) {
start = true;
win++;
System.out.println(" = " + sum + ";you win ");
break;
}
valuepoint = sum;
if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
|| valuepoint == 10) {
System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
break;
}
}
for (;;) {
if (sum == 7 || sum == 11) {
break;
} else {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
int f1 = d1;
System.out.print("\t" + "-you rolled again " + d1 + " " + plus);
for (int x = 0; x < 1; x++) {
d2 = 1 + rand.nextInt(6);
int f2 = d2;
int loda = f1 + f2;
System.out.print(" " + d2 + " = " + loda + "\n");
}
}
sum = d1 + d2;
if (sum == valuepoint) {
start = true;
win++;
System.out.print("\t" + "you win!" + "\n");
break;
} else if (sum == 7) {
loss++;
start = false;
System.out.print("\t" + "you lose " + "\n");
break;
}
}
}
System.out.print("wins: " + win + "\n");
System.out.print("Losses:" + loss);
}
public void play() {
for (int x = 0; x < 3; x++) {
rolldice();
play(d1, d2);
System.out.print("\n");
}
}
public void rolldice() {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
System.out.print("You rolled " + d1 + " " + plus);
}
for (int i = 0; i < 1; i++) {
d2 = 1 + rand.nextInt(6);
System.out.print(" " + d2 + " ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RandomSumGame Teet = new RandomSumGame();
RandomSumGame d1counter = new RandomSumGame();
RandomSumGame d2counter = new RandomSumGame();
Teet.play();
}
}
What part are you not able to finish? Seems like the game is running 3 times? Is it the total number of wins and loses? To fix that, you could just move "int win" and "int lose" outside the play method to make them global variables.
EDIT:
If you don't want to use global variables, you could make a method that calls itself (recursive method)
import java.util.Random;
public class RandomSumGame {
boolean start;
int d1;
int d2;
int sum = d1 + d2;
int valuepoint;
String plus = "+";
public void play(int d1, int d2, int win, int loss) {
sum = d1 + d2;
for (;;) {
if (sum == 2 || sum == 3 || sum == 12) {
start = false;
loss++;
System.out.println(" = " + sum + ";you lose");
break;
} else if (sum == 7 || sum == 11) {
start = true;
win++;
System.out.println(" = " + sum + ";you win ");
break;
}
valuepoint = sum;
if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
|| valuepoint == 10) {
System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
break;
}
}
for (;;) {
if (sum == 7 || sum == 11) {
break;
} else {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
int f1 = d1;
System.out.print("\t" + "-you rolled again " + d1 + " " + plus);
for (int x = 0; x < 1; x++) {
d2 = 1 + rand.nextInt(6);
int f2 = d2;
int loda = f1 + f2;
System.out.print(" " + d2 + " = " + loda + "\n");
}
}
sum = d1 + d2;
if (sum == valuepoint) {
start = true;
win++;
System.out.print("\t" + "you win!" + "\n");
break;
} else if (sum == 7) {
loss++;
start = false;
System.out.print("\t" + "you lose " + "\n");
break;
}
}
}
System.out.print("wins: " + win + "\n");
System.out.print("Losses:" + loss);
if (win < 2 && loss < 2) {
System.out.print("\n");
//Recursive
play(win, loss);
}
}
public void play(int win, int loss) {
rolldice();
play(d1, d2, win, loss);
}
public void rolldice() {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
System.out.print("You rolled " + d1 + " " + plus);
}
for (int i = 0; i < 1; i++) {
d2 = 1 + rand.nextInt(6);
System.out.print(" " + d2 + " ");
}
}
public static void main(String[] args) {
RandomSumGame test = new RandomSumGame();
test.play(0,0);
}
}
I have been working on this program for a few hours and the code is working correctly but I can't seem to get it to print out correctly, it should just be printing once for each value, such as:
number: 6
dividers: 2 3 6 1
prime: is not prime
Output
Can anyone help? Screenshot is attached. Thanks!
public static void main(String[] args) {
Random randomNums = new Random();
int count;
for (int i = 1; i <= 37; i++) {
count = randomNums.nextInt(100) + 1;
System.out.println("number " + count);
for (int b = 1; b<=count; b++) {
if (count % b == 0) {
System.out.println("dividers " + b);
}
}
for (int a = 2; a< count; a++) {
if (count % a == 0) {
System.out.println("is not prime");
}
if (count % a != 0) {
System.out.println("is prime");
}
}
}
}
}
try to write this code :
public static void main(String[] args) {
Random randomNums = new Random();
int count;
for (int i = 1; i <= 37; i++) {
count = randomNums.nextInt(100) + 1;
System.out.println("number " + count);
String dividers = "";
for (int b = 1; b<=count; b++) {
if (count % b == 0) {
dividers += b.toString() +" ";
}
}
// control the print beside loop
System.out.println("dividers " + dividers);
// add the control for whether prime
bool prime = true;
for (int a = 2; a< count; a++) {
if (count % a == 0) {
System.out.println("prime : is not prime");
// add the control for skip loop
prime = false;
break;
}
}
if(prime){
System.out.println("prime : is prime");
}
}
}
}
based upon your logic, I am guessing that if you decide that a number is not a prime then that is the final result,
so
boolean isPrime = true;
String dividers = "";
for (int a = 2; a< count; a++) {
if (count % a == 0) {
isPrime = false;
dividers += a+" ";
}
}
if (isPrime) {
System.out.println ("is Prime");
} else {
System.out.println ("dividers "+dividers);
System.out.println ("is not Prime");
}
How do I show the amount of True and falses. For example:
True - 8 is divisible by 2.
True - 42 is divisible by 2.
False - 11 is not divisible by 2.
(What I Want to it to show...)
True: 2
False: 1
Random r = new Random();
int[] num = new int[3]; // same as "= {0,0,0,0,0}
boolean gameResult = true;
for (int i = 0; i < num.length; i++) {
num[i] = r.nextInt(100) + 1;
if (num[i] % 2 == 0) {
System.out.println("TRUE - " + num[i] + " is divisible by 2.");
} else {
System.out.println("FALSE - " + num[i]
+ " is not divisible by 2.");
gameResult = false;
}
}
if (gameResult) {
System.out.println("You Won");
} else {
System.out.println("You Lost");
}
}
}
Random r = new Random();
int[] num = new int[3]; // same as "= {0,0,0,0,0}
int tCount = 0, fCount = 0;
boolean gameResult = true;
for (int i = 0; i < num.length; i++) {
num[i] = r.nextInt(100) + 1;
if (num[i] % 2 == 0) {
System.out.println("TRUE - " + num[i] + " is divisible by 2.");
tCount++;
} else {
System.out.println("FALSE - " + num[i]
+ " is not divisible by 2.");
fCount++;
gameResult = false;
}
}
System.out.println("True:" + tCount + "False:" + fCount);
if (gameResult) {
System.out.println("You Won");
} else {
System.out.println("You Lost");
}
}
}
The above code returns true and false counts. You can now use them as you want.
Here is the java code:
import java.util.Random;
public class DivisibleTest{
public static void main(String []args){
Random r = new Random();
int counterTrue=0;
int counterFalse=0;
int[] num = new int[3]; // same as "= {0,0,0,0,0}
boolean gameResult = true;
for (int i = 0; i < num.length; i++) {
num[i] = r.nextInt(100) + 1;
if (num[i] % 2 == 0) {
System.out.println("TRUE - " + num[i] + " is divisible by 2.");
counterTrue++;
counterTrue=counterTrue+1;
} else {
System.out.println("FALSE - " + num[i]
+ " is not divisible by 2.");
gameResult = false;
System.out.println(counterFalse);
counterFalse++;
counterFalse=counterFalse+1;
}
}
if (gameResult) {
System.out.println("You Won");
System.out.println("True:" +counterTrue );
System.out.println("False:" +counterTrue );
} else {
System.out.println("You Lost");
System.out.println("True:" +counterTrue );
System.out.println("False:" +counterTrue );
}
}
}
Let me know if you have any questions
I am new to programming. I was trying to make blackjack and I ran into some problems.
My console output:
Would you like to play again? y/n
y
You have: $50
Whats your bet:
12
You get a 7 and a 10
Your total is 17
The dealer has a 9 11 showing
Would you like to hit? y/n :
n
The dealer has 20 //Here it is supposed to say You lost blah blah blah
Would you like to play again? y/n //but its skipping to System.println("play again");
package loops;
import java.util.Scanner;
import java.util.Random;
public class loops {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
char hit = 0;
char playAgain = 0;
String dolphin = "banana";
int c = 5;
int win = 0;
int[] cards = {2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11};
int bet, money = 999999999, cardValue = 0, dealersValue = 0;
if(money == 999999999){
System.out.println("How much money would you like to start with?");
money = reader.nextInt();
}
do {
shuffleArray(cards);
System.out.println("You have: $" + money);
System.out.println("Whats your bet: ");
bet = reader.nextInt();
if(bet > money){
System.out.println("You cannot bet over " + money + "!");
System.exit(0);
break;
}
System.out.println("You get a " + cards[0] + " and a " + cards[1]);
cardValue = cards[0] + cards[1];
System.out.println("Your total is " + cardValue);
if(cardValue == 21){
win = 1;
}
if(cardValue > 21){
win = 2;
}
if(win != 2){
int i = 9;
System.out.println("The dealer has a " + cards[7] + " " + cards[8] + " showing");
dealersValue = cards[7] + cards[8];
System.out.println("Would you like to hit? y/n : ");
hit = reader.next().charAt(0);
while(dealersValue <= 17){
if(dealersValue <= 17){
dealersValue = cards[7] + cards[8] + cards[i];
}
if(dealersValue <= 17){
dealersValue = cards[7] + cards[8] + cards[i] + cards[i];
}
if(dealersValue <= 17){
dealersValue = cards[7] + cards[8] + cards[i] + cards[i] + cards[i];
}
if(cards[i] > 11 && dealersValue > 21){
cards[i] = 1;
}
if(cards[7] > 11 && dealersValue > 21){
cards[7] = 1;
}
if(cards[8] > 11 && dealersValue > 21){
cards[8] = 1;
}
i++;
}}
while(hit == 'y'){
System.out.println("You get a " + cards[c]);
cardValue = cardValue + cards[c];
System.out.println("Your total is " + cardValue);
if(cardValue > 21){
if(cards[5] > 11 && cardValue > 21){
cards[5] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}
if(cards[6] > 11 && cardValue > 21){
cards[6] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}
if(cards[7] > 11 && cardValue > 21){
cards[7] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}
if(cards[8] > 11 && cardValue > 21){
cards[8] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}
if(cards[9] > 11 && cardValue > 21){
cards[9] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}
if(cardValue > 21){
break;
}
}
System.out.println("Would you like to hit? y/n : ");
hit = reader.next().charAt(0);
c++;
if(hit =='n'){
System.out.println("You stay!");
cardValue = cardValue + 0;
break;
}
}
System.out.println("The dealer has " + dealersValue);
if(dealersValue > 21){
win = 1;
}
if(cardValue > 21){
win = 2;
}
if(cardValue == 21){
win = 1;
}
if(dealersValue == 21){
win = 2;
}
if (dealersValue > cardValue && dealersValue > 21){
win = 2;
}
if (dealersValue == cardValue){
win = 2;
}
if(dealersValue < cardValue && cardValue <=21) {
win = 1;
}
if(dealersValue == 21 && dealersValue != cardValue || dealersValue == 21 && dealersValue == cardValue || cardValue > 21){
win = 2;
}
if(cardValue == 21 && dealersValue != cardValue || dealersValue > 21){
win = 1;
}
if(win == 1){
System.out.println("You won "+bet*1 + " dollars!");;
System.out.println("Your total balance is " + money);
}
if(win == 2){
System.out.println("You lost "+bet*1 + " dollars!");;
System.out.println("Your total balance is " + money);
}
if(win == 3){
System.out.println("You lost "+bet*1 + " dollars!");;
System.out.println("Your total balance is " + money);
}
if(money <= 0){
System.out.println("You are out of money!");
System.exit(0);
}
System.out.println("Would you like to play again? y/n ");
playAgain = reader.next().charAt(0);
if(playAgain == 'n'){
System.exit(0); }
win = 0;
} while (playAgain == 'y');
}
static void shuffleArray(int[] cards){
Random rnd = new Random();
for (int i = cards.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
int a = cards[index];
cards[index] = cards[i];
cards[i] = a;
}
}
}
I've combed through the code multiple times, looking for where I went wrong, like maybe a } where it shouldn't be, but I could not find anything. I think it could use some fresh eyes!
After I get this code working, I'm planning on trying again with an object oriented approach.
The set of if statements that set the variable "win" are incorrect.
Walking through the code with the example given (dealer has 20, player has 17), the variable "win" is never set thus all the statements printing out "You lost" and "You won" are skipped.
It is very likely that in this line
if (dealersValue > cardValue && dealersValue > 21){
you meant
dealersValue < 21
which would then be cause win to be set to 2 and the correct output to be displayed.
Debugging Tip: Walk through the code with your IDE. If you step through line by line, you should immediately see it skip of setting the "win" variable.
Good luck in learning programming! It's fun but very frustrating :)
Recently, I've been working on my Noughts and Crosses game, but I've stumbled upon a problem that I can't seem to be able to fix.
Whenever I type in something the first time, it works and prints out the board, however, from that point onward, it just stops checking and printing out the board.
while(inf){
String temp = input.next();
if(playerTurn == 1){
if(amountP1 <= 3){
if(temp.equalsIgnoreCase("00")){
if(board[0][0] == 0){
board[0][0] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("01")){
if(board[0][1] == 0){
board[0][1] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("02")){
if(board[0][2] == 0){
board[0][2] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("10")){
if(board[1][0] == 0){
board[1][0] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("11")){
if(board[1][1] == 0){
board[1][1] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("12")){
if(board[1][2] == 0){
board[1][2] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("20")){
if(board[2][0] == 0){
board[2][0] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("21")){
if(board[2][1] == 0){
board[2][1] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("22")){
if(board[2][2] == 0){
board[2][2] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
}
else if(playerTurn == 2){
if(amountP2 <= 3){
if(temp.equalsIgnoreCase("00")){
if(board[0][0] == 0){
board[0][0] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("01")){
if(board[0][1] == 0){
board[0][1] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("02")){
if(board[0][2] == 0){
board[0][2] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("10")){
if(board[1][0] == 0){
board[1][0] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("11")){
if(board[1][1] == 0){
board[1][1] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("12")){
if(board[1][2] == 0){
board[1][2] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("20")){
if(board[2][0] == 0){
board[2][0] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("21")){
if(board[2][1] == 0){
board[2][1] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("22")){
if(board[2][2] == 0){
board[2][2] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
}
}
}
}
And this is the method for printing out the board:
public static void showBoard(int x[][]){
int rowNumber = 1;
for(int row = 0 ; row < x.length ; row++){
for(int column = 0 ; column < x[row].length ; column++){
if(x[row][column] == 0){
if(rowNumber <= 2){
System.out.print(" E");
rowNumber++;
}
else if(rowNumber == 3){
System.out.println(" E");
rowNumber = 1;
}
}
else if(x[row][column] == 1){
if(rowNumber <= 2){
System.out.print(" X");
rowNumber++;
}
else if(rowNumber == 3){
System.out.println(" X");
rowNumber = 1;
}
}
else if(x[row][column] == 2){
if(rowNumber <= 2){
System.out.print(" O");
rowNumber++;
}
else if(rowNumber == 3){
System.out.println(" O");
rowNumber = 1;
}
}
}
}
}
Also, I am aware that this code is a big mess and there's problably a ton of other, more efficient ways to do this, but since I am a fairly new programmer, I chose to go with functionality over efficiency with this one.
Your else if(playerTurn == 2) line needs to be pulled out to the same level as if(playerTurn == 1):
if(playerTurn == 1){
if(amountP1 <= 3){
//...
}
else if(playerTurn == 2){
//...
}
}
should become
if(playerTurn == 1){
if(amountP1 <= 3){
//...
}
} else if(playerTurn == 2) {
//...
}