0

Okay.

Here is my question. I am having some difficulty while making a dice game. The part I am having issue with is making it exit using a do while loop. Here is my code. and I'll explain what I mean further in

import java.util.Scanner;

public class Dice
{
 public static void main (String[] args)
 {
    int diceGuess;
    int rollNum, roll1, roll2, roll3;
    String playAgain = "yes";

    Scanner sc = new Scanner(System.in);


     // Create two separate dice objects to create a pair of dice
            Roll die1 = new Roll();
            Roll die2 = new Roll();

    // get from the user the number of dice rolls
            System.out.print ("Please enter a number between 2 and 12 to begin: ");
            diceGuess = sc.nextInt();

    do
    {       
        // loop to show rolls
        for (rollNum = 1; rollNum <=3; rollNum++)
          {
          System.out.println("**********Roll #: " + rollNum + " ************");
            roll1 = die1.roll();
            roll2 = die2.roll();

                //if statement to display you win if you win, and to make the loop break.
                if(diceGuess==roll1+roll2)  
                    {
                        System.out.println("You win!");
                        rollNum = 4;
                    }

                if(rollNum == 3)
                    {
                        System.out.println("You lose.\n");
                        System.out.println("Would you like to play again?");
                        playAgain = sc.nextLine();
                        rollNum ++;
                    }
            }           
        }while(playAgain == "yes");

}

}

This is my utility class. I want it to ask the user if they want to play again, and then if the user inputs no, I want it to quit. I'm sure this is just a minor misunderstanding on my part.
Thank you very much stackoverflow.

and heres the other class

//Justin Le
//Dice class


import java.util.Random;

public class Roll
{
//variables used in class
private Random randomRoll = new Random();
private int roll;
boolean playAgain = false;


//constructor
public Roll()
{
    roll = 0;
}

//method named roll to return roll number and inside, calls a method to display the roll.
public int roll()
{
    roll = randomRoll.nextInt(6) + 1;
    showRoll(roll);  //accesses showRoll to output in driver class.
    return roll;

}

public boolean test()
{
    playAgain = !playAgain;
    return playAgain;
}


//displays picture of dice roll
private void showRoll(int r)
    {
        switch(r)
            {
            case 1:
                System.out.println("One: \n" +
                                         " \n  "   +
                                         "  *  \n" +
                                         " \n  ");
            break;

            case 2:
                System.out.println("Two: \n" +
                                         "*    \n" +
                                         "     \n" +
                                         "    *\n");
            break;

            case 3:
                System.out.println("Three:\n" +
                                         "*    \n" +
                                         "  *  \n" +
                                     "    *\n");
            break;

            case 4:
                System.out.println("Four:\n" +
                                        "*   *\n" +
                                     "     \n" +
                                     "*   *\n");
            break;

            case 5:
                System.out.println("Five:\n" +
                                        "*   *\n" +
                                         "  *  \n" +
                                         "*   *\n");
            break;

            case 6:
                System.out.println("Six: \n" +
                                        "*   *\n" +
                                         "*   *\n" +
                                         "*   *\n");
            break;
            default:
                System.out.println("error\n");
    }

}

}

Justin Le
  • 673
  • 1
  • 8
  • 24

4 Answers4

4

In your while, use equals() method to compare Strings.. : -

playAgain.equals("yes")

See this excellent post for difference in comparison using equals() and ==..

In your first code: -

if(rollNum == 3)
{
    System.out.println("You lose.\n");
    System.out.println("Would you like to play again?");
    //playAgain = sc.nextLine();

    // Change your nextLine() to next()
    playAgain = sc.next();
    rollNum ++;
}
Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

change

playAgain == "yes"

to

"yes".equals(playAgain)
jmj
  • 237,923
  • 42
  • 401
  • 438
  • I did that, but I think one of the issues is this.. When it gets to the point where I enter yes or no... It just keeps giving me new lines to enter no no no no no – Justin Le Oct 06 '12 at 22:42
1

use while(plyaAgain.equals("yes"));

Follow this link for details on string comparision : How do I compare strings in Java?

Community
  • 1
  • 1
Jimmy
  • 2,589
  • 21
  • 31
0

Change this:

while(playAgain == "yes");

to this:

while(playAgain.equals("yes"));
Cliff Ribaudo
  • 8,932
  • 2
  • 55
  • 78