tags:

views:

79

answers:

3

I have to create a program in which a user can input a desired sum, then rolls a two six sided dices until their sum is the desired sum. I know that a) I have to use Scanner since it's an interactive program. b) I have to use indefinite loops to solve the problem including the use of random numbers. c) I don't expect you to give me the answer like I'm just copying and pasting. I just want to be guided on what to do so that my code actually compiles.

Here is my code:

import java.util.*;

 public class Game {
     public static void main(String[] args) {
         System.out.println("Try rolling two six-sided dices");
         System.out.println("until their sum is your");
         System.out.println("desired sum.");
         System.out.println();

         Scanner console = new Scanner (System.in);
         Random r = new Random();
         int sum = 0;
         int tries = 0;
         While (sum != number) {
             int roll1 = rand.nextInt(6) +1;
             int roll2 = rand.nextInt(6) +1;
             sum = roll1 + roll2;
             tries++;
         }
     }

         System.out.println("Desired dice sum: ");
         int number = console.nextInt();

*I keep getting these 3 compile errors:

Game.java:21: ';' expected While (sum != number) { ^ Game.java:29: expected
System.out.println("Desired dice sum: "); ^ Game.java:29: illegal start of type
System.out.println("Desired dice sum: "); ^ 3 errors


Edit:

Still getting 3 more compile errors

import java.util.*;

public class Game { public static void main(String[] args) { System.out.println("Try rolling two six-sided dice"); System.out.println("until their sum is your"); System.out.println("desired sum."); System.out.println();

     Scanner console = new Scanner (System.in);
     Random r = new Random();
     System.out.println("Desired dice sum: ");
     int number = console.nextInt();

     int sum = 0;
     int tries = 0;

     while (sum != number) {
         int roll1 = rand.nextInt(6) +1;
         int roll2 = rand.nextInt(6) +1;
         sum = roll1 + roll2;
         tries++;
     }
 }

}

@Matt:

This is the output that I should have, sorry for not being clear:

Desired dice sum: 9
4 and 3 = 7
3 and 5 = 8
5 and 6 = 11
5 and 6 = 11
1 and 5 = 6
6 and 3 = 9
+1  A: 
  1. It should be while, not While.

  2. You're missing a closing brace (}) at the end of your code.

  3. The (current) last two lines need to be inside of a method.

  4. You have not declared a variable named number properly. This is related to problem #3.

  5. You first call your random generator r, but later try to reference it by rand.


Try something like this (untested):

import java.util.*;

public class Game {
    public static void main(String[] args) {
        System.out.println("Try rolling two six-sided dices");
        System.out.println("until their sum is your");
        System.out.println("desired sum.");
        System.out.println();

        System.out.println("Desired dice sum: ");
        Scanner console = new Scanner (System.in);
        int number = console.nextInt();

        Random rand = new Random();
        int sum = 0;
        int tries = 0;
        while (sum != number) {
            int roll1 = rand.nextInt(6) +1;
            int roll2 = rand.nextInt(6) +1;
            sum = roll1 + roll2;
            tries++;
            System.out.println("Found sum: " + sum);
        }

        System.out.println("Done in " + tries + " tries");
    }
}
Matt Ball
Corrected. Thanks.
Makurian1
@Makurian1: see my edits. Helpful?
Matt Ball
I tried this and got 0 errors. Thank you!
Makurian1
@Makurian: it'll compile, certainly, but that doesn't mean the code is correct.
Matt Ball
So how do I make the outcomes add up and to show up as my output until it meets the user's desired sum?
Makurian1
Do i need a do on there as well?
Makurian1
@Makurian1: no, your code will find sums (and exit) just fine, but you need to print the output you want, I would assume as the last line of the `while` body. See my edit. Also, be careful about the input you accept. Your code will run forever if the user inputs a `sum` greater than 12. Do you understand why?
Matt Ball
Please see the edit. Yeah. I need to put an if/else statement on there to tell the user that they placed an incorrect input just to keep it incheck.
Makurian1
Or while loop I mean to handle user errors.
Makurian1
@Makurian1: okay, so the basic problem is that you stop searching for numbers that add up to the desired sum after you find the first two numbers that add up. Think about what you need to do to keep track of the matches you've already found, and print them out as you'd specified.
Matt Ball
Okay, gotcha. Thanks for the fixes!
Makurian1
@Makurian: reading over your question edits, it looks like I had misread what you were trying to do. I thought you were trying to find **all** combinations that add up to the desired sum. So, ignore my previous comment. All you need to do is print the number pairs as you "roll" the dice. You're really close!
Matt Ball
+3  A: 

Why do you have:

     System.out.println("Desired dice sum: ");
     int number = console.nextInt();

Out side of main? Put it back in.

Also, while is not the same as While change it to while.


In response to your new problems:

int roll1 = rand.nextInt(6) +1;

Should be something like:

int roll1 = r.nextInt(6) + 1;

Because you are making a variable r for your random not a variable rand.

thyrgle
Underneath which line please?
Makurian1
A: 

The last 2 lines:

System.out.println("Desired dice sum: ");
int number = console.nextInt();

are outside of your main. They need to be in main. If you want numbers to be a member of your class, declare it above your main, and initialize it in main, like:

private int number;
public static void main(String args[])
{
  ...
  number = console.nextInt();
  ...
}

Also, you can't make that System.out.println outside of a method. Put it in main.

prelic