2017-01-27 5 views
0

Ich versuche ein boolesches Flag zu erstellen, so dass der Benutzer 'M' eingeben muss (die Schülerantworten markieren), bevor sie 'S' und 'Q' wählen können. Wenn sie diese auswählen, sollte die Meldung "Bitte markieren Sie die Antworten vor der Eingabe von Statistiken" sein. Der Rest der Optionen sollte immer für den Benutzer verfügbar sein, aber ich bin mir nicht sicher, wie man einen Booleschen Wert einrichtet, so dass der Benutzer "M" wählen muss, bevor er "S" und "Q" wählt. Weiß jemand, wie man das macht? Hier ist mein Code so weit:Wie man einen boolean in einem Benutzermenü einrichtet?

public class Marker_Menu 
    { 
     public static void main(String args[])throws IOException 
     { 
      Quiz_Marker input2 = new Quiz_Marker(); 
      char arg[]= null; 

      System.out.println ("Welcome to the Quiz Grading System \n"); 

      char choice = menu(); 
      while(choice != 'E') 
      { 
       switch (choice) 
       { 
        case 'C': 
        input2.corAnsPrint(); 
        break; 

        case 'A': 
        input2.stuAnsPrint(); 
        break; 

        case 'M': 
        input2.quizMarking(); 
        break; 

        case 'S': 
        input2.stuStatsPrint(); 
        break; 

        case 'Q': 
        input2.quesStatsPrint(); 
        break; 

        default: 
        System.out.println("Your choice is invalid"); 
      } 
      choice = menu(); 
     } 
     System.out.println("Thank you for using the Quiz Marker System"); 
     System.exit(0);   
    } 

    public static char menu() throws IOException 
    { 


     System.out.println ("Please enter your choice \n" + 
      " C - Print Correct Answers \n" + 
      " A - Print Student Answers \n" + 
      " M - Mark the Student Answers \n" + 
      " S - Produce the Quiz Statistics \n" + 
      " Q - Produce Question Statistics \n" + 
      " E - Exit the System"); 

     Scanner input = new Scanner (System.in); 
     char choice = input.next().toUpperCase().charAt(0); 

     return choice; 

    } 
} 

Antwort

0

Nur eine boolean Variable erstellen, setzen Sie es, wenn 'M' ausgewählt ist, und es testen, ob 'S' oder 'Q' ausgewählt ist:

public static void main(String args[])throws IOException 
{ 
    boolean mSelected = false; 

    ... 

    while(choice != 'E') 
     { 
     switch (choice) 
     { 
      ... 

      case 'M': 
      mSelected = true; 
      input2.quizMarking(); 
      break; 

      case 'S': 
      if (mSelected) { 
       input2.stuStatsPrint(); 
      } else { 
       System.out.println("Please mark the answers before inputting statistics"); 
      } 
      break; 

      case 'Q': 
      if (mSelected) { 
       input2.quesStatsPrint(); 
      } else { 
       System.out.println("Please mark the answers before inputting statistics"); 
      } 
      break; 

      ... 
     } 

    ... 
+0

Dank! Es funktionierte. – Srk93