-1

I'm trying to add 2 random numbers (1-10) and the result has to be a positive number and no decimals.

Here is what I have come up with. It wont work and I'm not sure what to do.

public class MathGame {
    public static void main(String[] args) {
       int Low = 1;
       int High = 10;
       Random r = new Random();
       int Result = r.nextInt(High-Low) + Low;
    }
}
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Brandon.O
  • 27
  • 5

2 Answers2

0

You can just use:

Random r = new Random();
int result = 0;
for (int i = 0; i < 2; ++i) {
   result += (r.nextInt(10) + 1);
}

System.out.println(result);

If you want to be clearer about the particular numbers, try:

int num1 = r.nextInt(10) + 1;
int num2 = r.nextInt(10) + 1;

System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));

EDIT: in answer to a followup question, this approach will prompt a user to input an amount

public static void main(String[] args)
{
    Random r = new Random();

    final int MAX = 10;
    // get two random numbers between 1 and MAX
    int num1 = r.nextInt(MAX) + 1;
    int num2 = r.nextInt(MAX) + 1;

    int total = (num1 + num2);

    // display a question
    System.out.printf("What is your answer to %d + %d = ?%n", num1, num2);

    // read in the result
    Scanner stdin = new Scanner(System.in);
    int ans = stdin.nextInt();
    stdin.nextLine();

    // give an reply
    if (ans == total) {
        System.out.println("You are correct!");
    }
    else {
        System.out.println("Sorry, wrong answer!");
        System.out.printf("The answer is %d + %d = %d%n", num1, num2,
                (num1 + num2));
    }

}
KevinO
  • 4,303
  • 4
  • 27
  • 36
  • How would I input this without getting errors? – Brandon.O Apr 21 '16 at 02:26
  • @Brandon.O, what do you mean "input this"? – KevinO Apr 21 '16 at 02:27
  • When i put it into my program it says errors and it says cannot find symbol for Random r = new Random(); – Brandon.O Apr 21 '16 at 02:29
  • You must `import java.util.Random;`. No different than what you had defined before. – KevinO Apr 21 '16 at 02:30
  • Also, how would I make it so the user would have to input the answer. I know i would have to import scanner but how do i make sure they put in the correct one? – Brandon.O Apr 21 '16 at 02:38
  • @Brandon.O, I provided an updated answer that will obtain input from the user and check the result. If this answer addresses the question, please accept it. – KevinO Apr 21 '16 at 02:57
0

You need to import java.util.Random; and I would extract the logic to a method1. Something like,

import java.util.Random;

public class MathGame {
    private static final Random rand = new Random();

    private static int getRandomInt(int low, int high) {
        return rand.nextInt(high - low) + low;
    }

    public static void main(String[] args) {
        int a = getRandomInt(1, 10);
        int b = getRandomInt(1, 10);
        System.out.printf("%d + %d = %d%n", a, b, a + b);
    }
}

1And by convention, Java variable names start with a lower case letter.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249