import java.util.Random;
import java.util.Scanner;
public class Dividing {
public static void main(String[] args) {
int MAX = 10;
Random random = new Random();
int num1 = random.nextInt(MAX);
int num2 = 1 + random.nextInt(MAX);
int total = num1 * num2;
System.out.printf("What is your answer to %d / %d = ?%n", total, 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", total, num2, (total / num2));
}
}
}
Im trying to make a game where the user must solve a simple division problem. The 2 numbers are supposed to be randomly generated but the answer must be a whole number without negatives or decimals. In my code the only thing that will generate is zero divided by a number and rarely one divided by another number. Im not sure what I did wrong.
public static void main(String[] args) {
int MAX = 7;
Random random = new Random();
int total = random.nextInt(MAX) / 1;
int num2 = random.nextInt(MAX) / 1;
int num1 = total / num2;
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 / num2) {
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));
}
}
Aucun commentaire:
Enregistrer un commentaire