0

So for my class we are just learning to write methods. I am writing a game that rolls 2 dice, you win if they are the same, you lose if they are different. I am supposed to loop it until the user decides to quit. then i will print the number of rounds played and won/lost.It runs through once perfectly, but i am not sure how to loop until the user decides to quit. I cant loop whole methods, so im not sure what to do. i was thinking a boolean might somehow work in the main method but not sure. Here is what i have so far... Thanks.

import java.util.Scanner;

public class Project07 {

    public static void main(String[] args)
    {
        welcome();
        int r1 = rolldie();
        int r2 = rolldie();
        printdice(r1,r2);
        boolean b;      
        b = ispair(r1,r2);
        report(b);
    }

    public static void welcome()
    {
        System.out.println("Welcome to Computer Dice");
        System.out.println("You will first roll your dice");
        System.out.println("Next the outcome of your roll will be determined");
        System.out.println("any pair and you Win");
        System.out.println("anything else and you Lose");
        System.out.println();
        System.out.println();
        String y;
        Scanner stdIn= new Scanner(System.in);
        do{
            System.out.println("Press enter to roll");
            y=stdIn.nextLine();
        } while(!y.equals(""));

    }

    public static int rolldie()
    {
        int die;
        die = (int)(Math.random()*6+1);
        return die;
    }

    public static void printdice(int r1, int r2)
    {
        System.out.println("Your Roll: "+(r1) + (" ") + (r2) );
        System.out.println();
    }

    public static boolean ispair(int r1,int r2)
    {
        boolean b;
        if (r1==r2)
        {
            b=true;
        }
        else
        {
            b=false;
        }
        return b;
    }

    public static void report(boolean b)
    { 

        Scanner stdIn= new Scanner(System.in);
        String one;
        int gamesplayed=0;
        int sumwins=0;
        int sumlosses= 0;

        if(b==true)
        {
            System.out.println("You Win!");
            System.out.println();
            sumwins=sumwins+1;
        }
        else 
        {
            System.out.println("You Lose!");
            System.out.println();
            sumlosses=sumlosses+1;
        }
        gamesplayed=gamesplayed+1;

        do{
            System.out.println("Do you want to play again (y/n): ");
            one= stdIn.next();
        } while(!one.equalsIgnoreCase("y") && !one.equalsIgnoreCase("n"));

        if (one.equalsIgnoreCase("y"))
        {

        }
        else
        {
            System.out.println("You Played "+(gamesplayed)+ (" rounds"));
            System.out.println("You won "+(sumwins)+ (" rounds"));
            System.out.println("You lost "+(sumlosses)+ (" rounds"));
        }
    }
}
Ken Herbert
  • 5,205
  • 5
  • 28
  • 37

2 Answers2

0

Do whatever you need (rolling 2 dice), ask the user if wants to continue (Java: How to get input from System.console()) while the user still wants to do it.

Community
  • 1
  • 1
user3001267
  • 304
  • 1
  • 4
0

You could try something like this:

    String y;
    Scanner stdIn= new Scanner(System.in);
    do{
        System.out.println("Press enter to roll");
        y=stdIn.nextLine().charAt(0);
    } while(y.equals("y"));

Then if the user types anything besides a word which starts with y it will terminate the loop.

Greg
  • 737
  • 4
  • 13
  • 21