Hey I am trying to created a dice game that roll the dice twice and then ask the user if he/she thinks the next two rolls will be more than or less than the first two. After the third roll the program is then suppose to ask the user if he/she would like to change her guess. Then after the fourth row the output is suppose to either say "you won" or "you lost." I can't seem to figure out how to get the machine to print out more than or less than and to accept the different words of "MORE," or "more,"or "m," or "M" all for the more than value. I also need help when it comes to asking the user if he/she would like to change his/her guess and how to set this up. I appreciate your help. Here is my code so far.

import java.util.Scanner;

public class DiceGame2 
{

    public static void main(String[] args) 
    {
       Scanner keyboard = new Scanner(System.in);

       int aRandomNumber, resultOne, resultTwo;
       String moreThan, lessThan, userInput, userInput2, userInput3;
       aRandomNumber = (int) (Math.random()*6) + 1;{

       System.out.println(aRandomNumber);
       System.out.println(aRandomNumber);
       resultOne = (aRandomNumber + aRandomNumber);
       System.out.println("Will the sum of the next two rolls be more than "+
               "or less than " + resultOne+"?");
       userInput = keyboard.nextLine();

       System.out.println("Your third roll was "+ aRandomNumber +". Do you want"
               +" to change your guess?");

       userInput2 = keyboard.nextLine();

       resultTwo = (aRandomNumber + aRandomNumber);
       System.out.println("Your fourth roll is "+ aRandomNumber +". Your second " 
               +"total is "+resultTwo +".");
       }

    }

}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Code tags and formatting please. It's too hard to read otherwise:

[code]

// paste code here

[/code]

System.out.println("Your third roll was "+ aRandomNumber +". Do you want"
+" to change your guess?");

aRandomNumber is an integer. You need to convert it to a String with toString from the Integer class.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#toString(int)

Also, there are lots of good dice-rolling threads in Java on Daniweb. Type "Java dice" into Daniweb's search box and a lot will come up. They may not solve all of your problems, but it's very likely someone before has addressed something very similar.

Hey here is my version of it, the code is rather different, however I feel it acheives the same thing.

import java.io.*;
public class Dice
{
    public static void main (String[] args) throws IOException
    {
        BufferedReader inKey = new BufferedReader (new InputStreamReader (System.in));

        int ran, ran2, total, ran3, ran4, total2;

        ran = (int) (Math.random () * 6 + 1);
        ran2 = (int) (Math.random () * 6 + 1);
        total = ran + ran2;

        System.out.println ("Dice 1: " + ran);
        System.out.println ("Dice 2: " + ran2);
        System.out.println ("Total: " + total);
        System.out.println ();
        System.out.println ("Do you think the next roll will be less than or more(m = more & l = less) ");
        String choice = inKey.readLine ();

        System.out.println ();
        ran3 = (int) (Math.random () * 6 + 1);
        System.out.println ("Dice 3: " + ran3);

        System.out.println ("Would you like to change your mind? (m = more & l = less) ");
        choice = inKey.readLine ();
        System.out.println ();

        ran4 = (int) (Math.random () * 6 + 1);
        total2 = ran3 + ran4;

        if (choice.equals("m") && total2 > total)
        {
            System.out.println ("Dice 4: " + ran4);
            System.out.println ("Total 2: " + total2);
            System.out.println ("You won ");
        }
        else if (choice.equals("l")&& total2 < total)
        {
            System.out.println ("Dice 4: " + ran4);
            System.out.println ("Total 2: " + total2);
            System.out.println ("You won ");
        }
        else
        {
            System.out.println ("Dice 4: " + ran4);
            System.out.println ("Total 2: " + total2);
            System.out.println ("You lose ");
        }


    } // main method
} // Dice class

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.