Program problem: Write a program that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output.
Sample output: What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40 Correct. Nice work! Want more questions y or n ? y What is 5 * 5 ? 25 Correct. Nice work! Want more questions y or n ? y What is 8 * 9 ? 66 Incorrect. The product is 72 Want more questions y or n ? n You scored 3 out of 4
My code:
import java.util.Scanner;
public class Program3_3 {
private static String question;
private static Scanner input;
private static Scanner scanner;
//private static int answer2;
public static void main(String[] args) {
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
input = new Scanner(System.in);
System.out.print("What is " + number1 + " * " + number2 + "? ");
int answer = input.nextInt();
while (number1 * number2 != answer) {
System.out.print("Incorrect.Please try again. What is "
+ number1 + " * " + number2 + "? ");
answer = input.nextInt(); }
if (number1 * number2 == answer){
System.out.println("Correct. Nice work!");
scanner = new Scanner (System.in);
System.out.print("Want more questions yes or no? ");
question = scanner.next(); }
String yes = null;
if (question == yes){
input = new Scanner(System.in);
System.out.print("What is " + number1 + " * " + number2 + "? ");
int answer = input.nextInt();
}
}
}
Issue with my code:****I am having trouble giving the user an option to answer yes for more questions or no for the overall results. I understand the loop I have to create, but my program keeps giving me errors. Can someone please help me? and also check my overall code if its in good order.
Aucun commentaire:
Enregistrer un commentaire