In: Computer Science
Hello! I need to add a method that will display the total number of times the user answered the question correctly, how many time he was right or wrong and the percentage of time they were correct in answering. import java.util.Random; import java.util.Scanner; public class Test { private static int getUserInput() { int n; Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); return n; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String choice = "Yes"; Random random = new Random(); int[] userdata = new int[100]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println("write the number in half " + randomInt + "?"); userdata[count++] = randomInt; int userInput = getUserInput(); if (userInput == (randomInt / 2)) { System.out.println("That is correct!"); } else { System.out.println("That is incorrect!"); } System.out.print("Generate another random number?"); choice = scanner.next(); } int min = userdata[0], max = userdata[0]; for (int i = 0; i < count; i++) { if (userdata[i] > max) max = userdata[i]; if (userdata[i] < min) min = userdata[i]; } System.out.println("The highest random number you were given: " + max); System.out.println("The lowest random number you were given: " + min); } }
import java.util.Random;
import java.util.Scanner;
public class Test {
private static int getUserInput() {
int n;
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
return n;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String choice = "Yes";
Random random = new Random();
int[] userdata = new int[100];
int count = 0,right = 0,wrong = 0;
while (!choice.equals("No")) {
int randomInt = 2 * (random.nextInt(5) + 1);
System.out.println("write the number in half " + randomInt +
"?");
userdata[count++] = randomInt;
int userInput = getUserInput();
if (userInput == (randomInt / 2)) {
System.out.println("That is correct!");
right++;
}
else {
System.out.println("That is incorrect!");
wrong++;
}
System.out.print("Generate another random number?");
choice = scanner.next();
}
int min = userdata[0], max = userdata[0];
for (int i = 0; i < count; i++) {
if (userdata[i] > max) max = userdata[i];
if (userdata[i] < min) min = userdata[i];
}
System.out.println("The highest random number you were
given: " + max);
System.out.println("The lowest random number you were given: " +
min);
display(count,right,wrong);
}
private static void display(int count, int right, int
wrong) {
System.out.println("Right Answer: "+right);
System.out.println("Wrong Answer: "+wrong);
System.out.println("Right percent:
"+((float)right/count)*100+"%");
}
}
/* OUTPUT */
run:
write the number in half 4?
2
That is correct!
Generate another random number?Yes
write the number in half 2?
5
That is incorrect!
Generate another random number?No
The highest random number you were given: 4
The lowest random number you were given: 2
Right Answer: 1
Wrong Answer: 1
Right percent: 50.0%