简体   繁体   中英

How to get the scanner to keep asking for input until the correct input is entered? Java

I'm currently developing a math game for elementary students and I'm trying to let the user continuously enter an answer until they get the question correct. Then I want them to move onto the next question. When I run the code below it gives me 2 chances to enter the correct answer but it wont show my 'Correct' message after the second time.

import java.util.Scanner;
public class test{
    public static void main (String[]args){
        Scanner kb= new Scanner(System.in);
        double r5q1,r5q2,r5q3,r5q4,r5q5;
        System.out.println("Welcome to the money level.. This is the last and most difficult level!");
        //Question 1
        System.out.println("Question 1: I have $10.00 and I buy a candy bar for $3.00. How much change will I get?");
        r5q1=kb.nextDouble();

        if(r5q1==7)
            System.out.println("Correct!");
        else
            System.out.println("Try again!");
            r5q1=kb.nextDouble();
   }
}

This is a photo of my code

Put your input taking code inside do while loop and in while condition check for correct output

do{
    statements(s)
}while(condition);

try this.

import java.util.Scanner;

public class Main
{
    public static void main (String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int ans = 123;
        do {
            System.out.println("Your question here.\n");
        } while (cin.nextInt() != ans);
        System.out.println("correct!\n");
    }
}
while(userAnswer != realAnswer){
    System.out.print("Wrong answer. Try again: ");
    userAnswer = in.nextInt(); // or whatever you need instead of int
}

You have to use a while loop. This loop repeats its "contents" (the code inside the curly brackets) while its condition (the boolean value inside the normal brackets) is true. In this case, the condition is that the input is in correct and the "content" is the code which prints the "Try again!" message. Here's a demonstration:

Scanner scanner = new Scanner(System.in);
while (scanner.nextDouble() != 7) {
    System.out.println("Try again!");
}
System.out.println("Correct!");

declare a flag value. And initially set it to 0. flag=0;

    do{
     if(r5q1==7){
     flag=1;
     break;
     }
     else{
     continue;
      }
    }while(r5q1!=7);

    if(flag==1)
    {
    System.out.println("Correct");
    }
    else
    {
    System.out.println("Wrong");
    }

Thanks admin! That worked very well!

import java.util.Scanner;
public class test{
   public static void main (String[]args){
      Scanner kb= new Scanner(System.in);
      double r5q1,r5q2,r5q3,r5q4,r5q5;
      System.out.println("Welcome to the money level.. This is the last and most difficult level!");
      //Question 1
      System.out.println("Question 1: I have $10.00 and I buy a candy bar for $3.00. How much change will I get?");
      r5q1=kb.nextDouble();
         do{
            System.out.println("Try again!");
            r5q1=kb.nextDouble();
         }while(r5q1!=7);//This is the answer to the question
      System.out.println("Correct!");

      //Question 2
      System.out.println("Question 2: I have $15.00 and I buy 5 apples for $1.00 each. How much change will I get?");
      r5q1=kb.nextDouble();
         do{
            System.out.println("Try again!");
            r5q2=kb.nextDouble();
         }while(r5q2!=10); //This is the answer to the question
      System.out.println("Correct!");
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM