Results 1 to 3 of 3

Thread: do while help?

  1. #1
    AyuzawaM is offline Member
    Join Date
    Aug 2014
    Posts
    12
    Rep Power
    0

    Default 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
                                        } 
                                }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default 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

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default Re: do while help?

    anyway you have already declared a scanner object.using nextLine() method get yes or no from user and control ur while loop.
    Ramya:cool:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •