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 totalScore;
private static int answer1;
private static int userScore;
public static void main(String[] args) {
do {
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
totalScore = 0;
userScore = 0;
input = new Scanner(System.in);
System.out.print("What is " + number1 + " * " + number2 + "? ");
int answer = input.nextInt();
while (number1 * number2 != answer) {
int answer1 = number1 * number2;
System.out.println("Incorrect. The answer is " + answer1+".");
System.out.print("Want more questions yes or no? ");
scanner = new Scanner (System.in);
question = scanner.next();
break;
}
if (number1 * number2 == answer){
System.out.println("Correct. Nice work!");
System.out.print("Want more questions yes or no? ");
scanner = new Scanner (System.in);
question = scanner.next();
}
if (question.equals("no")||question.equals("n")&& (number1 * number2 == answer)){
{
userScore++;
totalScore++;
System.out.println("You have this many questions correct:" + totalScore);
}
}
while(question.equals("yes") || question.equals("y"));}
}
Code issues: I am having a lot of trouble with this code. I feel like my program is overly complex for the task. The output is not generating the amount of correct answers when the answer to "do you want more questions" is no and the brackets at the end are giving me lots of errors when I run the program. The other portion of the code should be correct, but the very last step is causing issues. Could someone explain why?
Aucun commentaire:
Enregistrer un commentaire