Results 1 to 3 of 3
Thread: do while help?
- 08-21-2014, 11:40 AM #1
Member
- Join Date
- Aug 2014
- Posts
- 12
- Rep Power
- 0
do while help?
Okay first things first here is some background information about what the program is about. So I'm writing a program that is about rolling "dice" and getting the scores to add up to 100. There are two players in the game and each must take turns rolling dice choosing whether or not to keep rolling dice and accumulating more points during their turn while risking their points being lost by rolling a 1 or to add their turn points to their total points. The question I have is how would I exit the do while loop if the player chooses to add their turn score, thus adding their score and ending their turn? Thanks for help in advance
Here is the coding so far.
Java Code:import java.util.Scanner; import java.util.Random; public class Pig { public static void main(String[] args) { int die; int userTotalScore; int userTurnScore; int compTotalScore; int compTurnScore; String = choice Scanner keyboard = new Scanner(System.in); Random die = new Random(); System.out.print("Welcome to the game of Pig!"); userTotalScore = 0 compTotalScore = 0 userTurnScore = 0 compTurnScore = 0 while (gameOver == false) { // human’s turn do { roll dice if (die == 1) { System.out.println("You lose your turn!") // display “You lose your turn!” and output current total //set turn over to true and exit do-while loop } else { userTurnScore += die userTotalScore += userTurnScore System.out.println("Your turn score is " + userTurnScore + " and your total score is " + userTotalScore); System.out.println("If you hold, you will have " + userTotalScore) System.out.println("Enter r to roll again, h to hold") choice = keyboard.nextLine(); // accumulate turn score //display current turn score and total score // ask user’s input whether to roll again or hold? if (choice == ‘h’) //set turn over to true and exit do-while loop } } while (turn is not over) add in any points the human got and output current total check for human winning if (userTotalScore >= 100) { display “YOU WIN!” set game over to true and exit the while loop } // computer’s turn do { roll dice if (die == 1) { display “The computer lost its turn!” and output total set turn over to true and exit do-while loop } else { accumulate turn score if (turn score >= 20 or total plus turn score >= 100) display “The computer holds” and set turn over true } } while (turn is not over) add in any points the computer got and output current total check for computer winning if (computer’s score >= 100) { display “THE COMPUTER WINS!” set game over to true and exit the while loop } }
- 08-21-2014, 12:03 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: do while help?
... just like you exit your outer game loop, by changing a boolean variable?
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-21-2014, 12:07 PM #3
Bookmarks