2016-05-22 7 views
-1

Ich bin völlig neu Programmierung und bin nur herumspielen mit dem, was ich bisher weiß. Ich mache eine primitive "KI", die einfache Fragen stellt. In diesem Teil des Codes fragt das Programm "Wo lebst du?" und "Wohnen Sie gerne dort?" Ich versuche, es so zu machen, dass, wenn der Benutzer alles außer Variationen von Nein oder Ja eingibt, der Bot die Frage erneut stellen wird. Hier ist, was ich bis jetzt habeSehr nooby Anfrage Eingang und wenn Aussagen

 System.out.println("I have never been to " + line3 + " before. Do you like it there?"); 

    // Wait for user to enter a line of text 
    String line4 = input.nextLine(); 

    if(line4.equals("Yes")){ 
     System.out.println("That's good! I'm glad to hear it."); 
    } 
     else if(line4.equals("No")){ 

      System.out.println("Oh no! You should move then!"); 

     } 
     else if(line4.equals("no")){ 

      System.out.println("Oh no! You should move then!"); 

     } 
     else if(line4.equals("yes")){ 

      System.out.println("That's good! I'm glad to hear it."); 

     } 

Wie würde ich darüber gehen? Gibt es einen einfacheren Weg, um das zu erreichen, was ich bereits im Code geschrieben habe? Ich müsste mir sonst noch eine Aussage machen für jedes andere Ja/Nein, zum Beispiel würde ich einen anderen brauchen, wenn für "Ja!" oder "überhaupt nicht!" etc. etc.

Vielen Dank im Voraus

+0

Ihr Code ist nicht vollständig, bitte legen Sie vollständigen Code ..... Anstelle von separaten "Nein" und "Nein", können Sie ignorieren Fall verwenden. – piyushj

Antwort

1

Eine Möglichkeit, eine Endlos-Schleife verwenden (und equalsIgnoreCase); wie

String line4 = input.nextLine(); 
while (true) { 
    if (line4.equalsIgnoreCase("yes")) { 
     System.out.println("That's good! I'm glad to hear it."); 
     break; 
    } else if (line4.equalsIgnoreCase("no")) { 
     System.out.println("Oh no! You should move then!"); 
     break; 
    } 
    line4 = input.nextLine(); 
} 

Eine weitere Option, verwenden Sie einen Map<String, String> und eine Schleife wie

Map<String, String> msgMap = new HashMap<>(); 
msgMap.put("yes", "That's good! I'm glad to hear it."); 
msgMap.put("no", "Oh no! You should move then!"); 
// ... 
String line4; 
do { 
    line4 = input.nextLine().toLowerCase(); 
    if (msgMap.containsKey(line4)) { 
     System.out.println(msgMap.get(line4)); 
    } 
} while (!msgMap.containsKey(line4)); 

und eine andere Option, wie die Eingabeaufforderung und Eingabe in eine Hilfsmethode extrahieren

static boolean getYesOrNo(Scanner scanner, String msg) { 
    while (true) { 
     System.out.println(msg); 
     String v = scanner.nextLine(); 
     if (v.equalsIgnoreCase("yes")) { 
      return true; 
     } else if (v.equalsIgnoreCase("no")) { 
      return false; 
     } 
    } 
} 

Dann könnten Sie rufen es mag

if (getYesOrNo(input, "Do you like living there?")) { 
    System.out.println("That's good! I'm glad to hear it."); 
} else { 
    System.out.println("Oh no! You should move then!"); 
} 
0

Sie können Funktion in eine separate Methode bewegen stellen und es in Rekursion (oder Cobine es mit Antwort Elliott Frisch):

public static boolean askUser(String question) { 
    System.out.println(question); 
    String answer = input.nextLine(); 
    if ("yes".equalsIgnoreCase(answer)) { 
     return true; 
    } else if ("no".equalsIgnoreCase(answer)) { 
     return false; 
    } else { 
     return askUser(question); 
    } 
} 

Sie es auf diese Weise also jetzt verwenden können:

String prevLine = "Boston"; 
    String question = "I have never been to " + prevLine + " before. Do you like it there?"; 
    boolean answer = askUser(question); 
    if (answer) { 
     System.out.println("That's good! I'm glad to hear it."); 
    } else { 
     System.out.println("Oh no! You should move then!"); 
    } 

Oder wickeln Sie es wieder in eine Karte