This question already has an answer here:
I'm making a game where the user must solve a simple subtraction but the result must be a positive whole number. I managed to do everything but for some reason the answer is sometimes negative and I'm not sure how to fix it
import java.util.Random;
import java.util.Scanner;
public class Subtraction {
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));
}
}
}
Generate your first value with room at the bottom for a second value to be subtracted, and the second one from a range bounded by the first:
The first number will always be strictly larger than the second, by construction, so the difference will always be a positive integer.
The code at the very heart of your program is wrong:
r.nextInt(MAX)
returns a number between 0 (inclusive) andMAX
(exclusive). Your code subtracts one from it, so you get a number in the range[−1, MAX−2]
.Since you want it to be a simple subtraction where all numbers are in the range
[1, MAX]
, you have to generate them that way. The general form of the subtraction is:This equation has the following constraints:
1 <= result <= MAX
1 <= num1 <= MAX
1 <= num2 <= MAX
result < MAX
, since otherwisenum2
would have to be0
1 < num1
, since otherwise the result would become negativenum2 < MAX
, since otherwiseresult
would have to be larger thanMAX
This leaves the following allowed ranges:
1 <= result <= MAX − 1
2 <= num1 <= MAX
1 <= num2 <= MAX − 1
num2 <= num1 - 1
To generate these numbers, the code has to look like this:
Now what is
randomBetween
? You have to define it:Together, this is:
Two possible solutions: Just change your total line with a condition to subtract the larger one from the smaller one (unless they're the same, in which case you'll get 0)
Or just use the absolute value:
Change the printf as well:
The conditionals are just making sure that the bigger number comes before the smaller number, or if they happen to be equal, that they are both listed.
Check out http://www.cafeaulait.org/course/week2/43.html for a more thorough explanation of the ? operator.
Since you know the result must be positive, I would start with the result
This way you can be sure that
num1 - num2
will always be positive.Well, with two random numbers in the same range, in random order, either cold be larger and the subtraction could be negative. Either fix how you get the numbers, or fix how they are ordered, or fix how you get their difference; any of these will do the job.