In: Computer Science
I need to display the questions that the user answered total, wrong and right as well as the average percentage they got correct of the following code please!
public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); while(!choice.equals("No")){ int randomInt = 2 * (random.nextInteger(5) + 1); System.out.println(randomInteger); System.out.print("Want another random number (Yes / No)? "); choice = scanner.next(); } } }
import java.util.Random; import java.util.Scanner; //TestCode.java public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); int correct = 0, wrong = 0, total = 0; while(!choice.equals("No")){ int randomInt1 = 2 * (random.nextInt(5) + 1); int randomInt2 = 2 * (random.nextInt(5) + 1); System.out.print("What is "+randomInt1+" + "+randomInt2+"? "); int res = scanner.nextInt(); if(res == (randomInt1+randomInt2)){ correct++; } else{ wrong++; } total++; System.out.print("Want another question (Yes / No)? "); choice = scanner.next(); } System.out.println("Number of correct answers = "+correct); System.out.println("Number of wrong answers = "+wrong); System.out.println("Average percentage you got = "+(100.0*correct/total)); } }