2017-11-23 2 views
0

Ich entwickle gerade ein Mathe-Spiel für Grundschüler und ich versuche, den Benutzer kontinuierlich eine Antwort eingeben zu lassen, bis sie die Frage richtig beantwortet haben. Dann möchte ich, dass sie zur nächsten Frage übergehen. Wenn ich den Code unten ausführe, gibt es mir 2 Möglichkeiten, um die richtige Antwort einzugeben, aber es zeigt nicht meine 'Korrekte' Nachricht nach dem zweiten Mal.Wie wird der Scanner dazu gebracht, nach Eingabe zu fragen, bis die richtige Eingabe eingegeben wird? Java

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

Antwort

0

Setzen Sie Ihre Eingabe unter Code innerhalb tun while-Schleife und in während Bedingungsprüfung für die korrekte Ausgabe

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

dies versuchen.

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"); 
    } 
} 
0
while(userAnswer != realAnswer){ 
    System.out.print("Wrong answer. Try again: "); 
    userAnswer = in.nextInt(); // or whatever you need instead of int 
} 
0

Sie haben eine while-Schleife zu verwenden. Diese Schleife wiederholt ihren "Inhalt" (den Code innerhalb der geschweiften Klammern), während ihre Bedingung (der boolesche Wert innerhalb der normalen Klammern) wahr ist. In diesem Fall lautet die Bedingung, dass die Eingabe in korrekt ist und der "Inhalt" der Code ist, der das "Erneut versuchen!" Botschaft. Hier ist eine Demonstration:

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

einen Flag-Wert erklären. Und setze es anfangs auf 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"); 
    } 
0

Danke admin! Das hat sehr gut funktioniert!

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!"); 
    } 
} 
Verwandte Themen