Hello!
I recently was inspired by my friend's inspiration of someone he knew to write a code that simulated a game that was created by my friend's friend in Minecraft. *whew*. The game was supposed to have eight torches lined up in a row, and you were supposed to choose a number between 1 and 3. The computer would also choose a number between 1 and 3, and the total number of torches would be lit up. You would continue in this way until you either hit eight torches lit exactly, or you went over eight torches lit, in which case you would lose.
The code itself works fine, except for the fact that as soon as I was finished, I decided to impress my friend by adding the function of the the computer asking the user if he/she wants to play again.
Not so simple.
After I had finally gotten the code running after copying and pasting the code that asks the user a question and adding the break; to it, the IDE terminates the code if I enter true or false!
Maybe I could put the boolean value into a string and say:
if (playAgain=="yes") {
playAgain=true
break;
}
else {
playAgain=false
break;
}
Here is the actual code:
import java.util.Scanner; public class EightTorches { public static void main(String[] arg) { int com1, com2, com3, com4; int num1, num2, num3, num4; int totalTorches; boolean playAgain=true; Scanner scan = new Scanner(System.in); do { System.out.print("This is a game where there are eight torches. You pick a number between 1 and 3 and the computer picks a number. That total number of torches is lit up. I f you light exactly eight torches, you win! However, if you light more than eight torches, you lose. The game will continue to ask for numbers until you have either won or lost. Have fun!"); System.out.println(); System.out.println(); System.out.print("What is you first guess? "); com1= (int) (Math.random()*3)+1; num1= scan.nextInt(); totalTorches=num1+com1; if (totalTorches==8) { System.out.print("The total number of torches is equal to eight! You win!"); System.out.println(); System.out.println(); System.out.print("Would you like to play again? "); playAgain= scan.nextBoolean(); break; } else if (totalTorches>8) { System.out.print("You have too many torches. You lose."); System.out.println(); System.out.println(); System.out.print("Would you like to play again? "); playAgain= scan.nextBoolean(); break; } else if (totalTorches<8) { System.out.print("Eight torches have not been lit up yet."); System.out.println(); System.out.println(); System.out.print("What is your next guess?"); } com2= (int) (Math.random()*3)+1; num2= scan.nextInt(); totalTorches=num2+com2+num1+com1; if (totalTorches==8) { System.out.print("The total number of torches is equal to eight! You win!"); System.out.println(); System.out.println(); System.out.print("Would you like to play again? "); playAgain= scan.nextBoolean(); break; } else if (totalTorches>8) { System.out.print("You have too many torches. You lose."); System.out.println(); System.out.println(); System.out.print("Would you like to play again? "); playAgain= scan.nextBoolean(); break; } else if (totalTorches<8) { System.out.print("Eight torches have not been lit up yet."); System.out.println(); System.out.println(); System.out.print("What is your next guess? "); } com3= (int) (Math.random()*3)+1; num3= scan.nextInt(); totalTorches=num3+com3+num2+com2+num1+com1; if (totalTorches==8) { System.out.print("The total number of torches is equal to eight! You win!"); System.out.println(); System.out.println(); System.out.print("Would you like to play again? "); playAgain= scan.nextBoolean(); break; } else if (totalTorches>8) { System.out.print("You have too many torches. You lose."); System.out.println(); System.out.println(); System.out.print("Would you like to play again? "); playAgain= scan.nextBoolean(); break; } else if (totalTorches<8) { System.out.print("Eight torches have not been lit up yet."); System.out.println(); System.out.println(); System.out.print("What is your next guess? "); } com4= (int) (Math.random()*3)+1; num4= scan.nextInt(); totalTorches=num4+com4+num3+com3+num2+com2+num1+com1; if (totalTorches==8) { System.out.print("The total number of torches is equal to eight! You win!"); System.out.println(); System.out.println(); System.out.print("Would you like to play again? "); playAgain= scan.nextBoolean(); break; } else if (totalTorches>8) { System.out.print("You have too many torches, or you did not equal eight. You lose."); System.out.println(); System.out.println(); System.out.print("Would you like to play again? "); playAgain= scan.nextBoolean(); break; } } while (playAgain = true); } }
The boolean value is supposed to tell the computer whether or not to replay the do..while loop.
Advice?
-Silent