Results 1 to 3 of 3
- 11-21-2009, 09:57 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
Unreachable statement Dice game
I need some help. For some reason whenever i run this program, it says that the statement on ln 123 can't be reached. Here is the program. Its a dice game.:):):)
Java Code:import java. util.Scanner; public class PigDiceGame { public static void main ( String[] args ) { System.out.println("You are Going to Play the Pig Dice Game!"); System.out.println(""); System.out.println(""); System.out.println("Do You Know the rules?...if not press 1...otherwise press any key to continue."); Scanner sc = new Scanner (System.in); int rules = sc.nextInt (); if (rules == 1) { System.out.println(""); System.out.println(""); System.out.println("Rules..."); System.out.println(""); System.out.println(""); System.out.println("1. In one turn the player rolls the dice until either"); System.out.println(" -They roll a one"); System.out.println(" -Or they choose to stop"); System.out.println("2. You add up the numbers on the dice and are trying to reach 100."); System.out.println("3. Rolling a 1 will clear your points for that turn."); System.out.println("4. If you choose to stop you can keep the points you have earned."); System.out.println("5. Rolling a pair counts double points"); System.out.println(""); System.out.println(""); } System.out.println("Lets begin..."); System.out.println(""); System.out.println(""); PairOfDice player1= new PairOfDice(); PairOfDice player2= new PairOfDice(); boolean game=true; boolean round=true; int player1GameScore=0; int player2GameScore=0; int player1roundScore=0; int player2roundScore=0; int turnScore=0; int hold=1; System.out.print("If player1 is going to go first press 1, if player 2 is going to go press 2 ::"); int turn = sc.nextInt (); while (game=true) { while (true) { while(turn==1&&player1roundScore<=100-player1GameScore) { System.out.println(""); System.out.print("Are you ready Player 1. Press 1 roll.(All other number are not allowed) :: "); int ready = sc.nextInt (); player1.roll(); player1.print(); player1.totalofdice(); if (player1.totalofdice()==0) { player1roundScore=player1.totalofdice(); System.out.println("Player 1 have lost all your points"); System.out.println("Player 1 have earned"+player1roundScore+"points"); turn++; } else { turnScore=player1.totalofdice(); player1roundScore=player1roundScore+turnScore; System.out.println("Player 1 has earned"+player1roundScore+"points"); System.out.print("To continue press 1 or press 2 if you wish to hold(bank) the points earned :: "); hold = sc.nextInt (); if (hold==2) { turn++; } } } player1GameScore=player1GameScore+player1roundScore; System.out.println("Player 1's new total score is"+player1GameScore+"points"); while(turn==2&&player2roundScore<=100-player1GameScore) { System.out.println(""); System.out.print("Are you ready Player 2. Press 1 roll. :: "); int ready = sc.nextInt(); player2.roll(); player2.print(); player2.totalofdice(); if (player2.totalofdice()==0) { player2roundScore=player2.totalofdice(); System.out.println("Player 2 has lost all your points"); System.out.println("Player 2 has earned "+player2roundScore+" points"); turn--; } else { turnScore=player2.totalofdice(); player2roundScore=player2roundScore+turnScore; System.out.println("Player 2 has earned "+player2roundScore+" points"); System.out.print("To continue press 1 or press 2 if you wish to hold(bank) the points earned :: "); hold = sc.nextInt(); if (hold==2) { turn--; } } } player2GameScore=player2GameScore+player2roundScore; player1roundScore=0; player2roundScore=0; System.out.println("Player 1's new total score is"+player1GameScore+"points"); System.out.println("Player2's new total score is"+player2GameScore+"points"); if (player1GameScore>100) System.out.println("Player 1 wins"); if (player2GameScore>100) System.out.println("Player 2 wins"); } System.out.println("do you want to play again Press 1 if you do"); [COLOR="Sienna"][B][I][U]This line[/U][/I][/B][/COLOR] int play= sc.nextInt (); if (play==1) game=true; else game=false; } } }
Your help is greatly appreciated. By the way here the class file.
Thanks:):)Java Code:class PairOfDice { private Die die1; private Die die2; private int total=0; public PairOfDice() { die1= new Die(); die2= new Die(); } public void roll() { die1.roll(); die2.roll(); } public int totalofdice() { if (die1.getFaceValue()==die2.getFaceValue ()) { if (die1.getFaceValue ()== 1 && die2.getFaceValue ()== 1) { total=25; } if (die1.getFaceValue ()== 2 && die2.getFaceValue ()== 2) { total=9; } if (die1.getFaceValue ()== 3 && die2.getFaceValue ()== 3) { total=12; } if (die1.getFaceValue ()== 4 && die2.getFaceValue ()== 4) { total=16; } if (die1.getFaceValue ()== 5 && die2.getFaceValue ()== 5) { total=20; } if (die1.getFaceValue ()== 6 && die2.getFaceValue ()== 6) { total=24; } } else { if(die1.getFaceValue()==1||die2.getFaceValue()==1) total=0; else total=die1.getFaceValue()+die2.getFaceValue(); } return total; } public void print() { System.out.println ("The first die rolled a:"+die1.getFaceValue ()+ " The second die rolled a:"+ die2.getFaceValue ()); } }Last edited by noturn10; 11-22-2009 at 12:25 AM.
-
So in abbreviation, your code is structured something like this:
So the compiler is helping you out here, because there in fact is no way for that line to be reached seeing as it follows a while (true) {} block.Java Code:while (game = true) { while (true) { //... bunch of code here } System.out.println("do you want to play again Press 1 if you do"); // This line //... more code here
Solution: fix your code so that it is reachable.
Much luck!
- 11-22-2009, 12:36 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Help with a dice game.
By hero in forum AWT / SwingReplies: 14Last Post: 07-26-2009, 11:50 AM -
Unreachable Statement (accessors) - works as a main method!
By thomase in forum New To JavaReplies: 6Last Post: 03-11-2009, 04:38 PM -
unreachable statement - Java calculator program
By V2001Gordon in forum New To JavaReplies: 3Last Post: 12-13-2008, 12:57 AM -
Help debugging a dice game
By Windoze in forum New To JavaReplies: 7Last Post: 11-22-2007, 01:01 AM -
help debugging a dice game
By Windoze in forum Advanced JavaReplies: 0Last Post: 11-16-2007, 10:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks