Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8662641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:46:57+00:00 2026-06-12T16:46:57+00:00

Okay. Here is my question. I am having some difficulty while making a dice

  • 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");
    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:46:58+00:00Added an answer on June 12, 2026 at 4:46 pm

    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 ++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope you're having a good Friday and stuff... okay, so here's my question: All
Okay here's the question, using C# I need to create a program using a
Okay. So here's my question: I am making a data parser in Clojure. One
Okay so here is the deal. As the question states, I'm trying to POST
Okay this is definitely a n00b question but here goes. The way I understand
Okay, here's my short question: I know that === and !== operators will compare
Okay so here's what I'm doing. I'm making a request to a server to
I asked a similar question here a while back but all the answers were
Okay first of all, I admit I took elements from this question here: How
Okay. I'm having some conceptual issues with a WCF service i want to create.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.