2016-06-05 4 views
-1

Hallo, alles, was ich versuche, eine Schleife in meinem Code zu schreiben, der Benutzer auffordern würde, wenn sie etwas eingeben, das nicht vordefiniert ist. Ich bin etwas vertraut mit dieser Benutzereingabe, die kein spezifisches Wort oder int ist, aber nicht sicher, wenn der Benutzer drei Auswahlmöglichkeiten hat.So validieren Sie die Zeichenfolge von der Benutzereingabe zum vordefinierten Namen

import java.util.Arrays; 
import java.util.List; 
import java.util.Scanner; 

// this class will instantiates player object 
public class Adventure { 

    public static void main(String[] args) { 
     // main method 

     System.out.println("Hello and welcome to my text adventure! "); 
     Scanner myinput = new Scanner(System.in); 
     Player gameplayer = new Player(); // create a player object and assign it to gameplayer 
     System.out.print("Please enter your name.\n"); 
     String nameofPlayer = myinput.nextLine(); 
     gameplayer.setPlayer(nameofPlayer); 
     System.out.print("Please enter your class. (Mage, Archer, Warrior)\n"); 
     List<String> names = Arrays.asList("mage","archer","warrior"); 
     String userinput; 
      while (myinput.hasNext()) { 
       userinput = myinput.nextLine(); 
       String nameofClass = userinput.toLowerCase(); 
       if (!names.contains(nameofClass)) { 
        System.out.println("I'm sorry, what was that?"); 

       } else { 
        gameplayer.setclassName(nameofClass); 
        System.out.println("Hello " + gameplayer.getPlayer() + " the " 
        + gameplayer.getClassName()+ ". What health do you have?"); 
       } 

       } 
        int healthofPlayer ; 

        while (myinput.hasNextInt()){ 
         healthofPlayer = myinput.nextInt(); 

        if ((!myinput.hasNextInt())){ 
         System.out.println("I'm sorry, what was that?"); 
        } 
        else { 
         gameplayer.setHealth(healthofPlayer); 
         System.out.println("Very good. Now let's get started on your adventure."); 
         System.out.println("You awake alone, disoriented, and locked in the CS1331 TA Lab."); 


        } 
        return; 
      } 



      } 

} 
+0

meine Frage kann den gleichen Inhalt haben, aber es ist nicht in der Nähe dieses Threads. – BabyC0d3eR

Antwort

-1

ausprobieren, das sollte den Rest ausarbeiten.

public static void main(String[] args) { 
// main method 

System.out.println("Hello and welcome to my text adventure! "); 
List<String> names = Arrays.asList("mage","archer","warrior"); 
Scanner myinput = new Scanner(System.in); 
Player gameplayer = new Player(); // create a player object and assign it to gameplayer 
System.out.print("Please enter your name.\n"); 
String nameofPlayer = myinput.nextLine(); 
gameplayer.setPlayer(nameofPlayer); 


System.out.print("Please enter your class. (Mage, Archer, Warrior)\n"); 
String userinput; 
while (myinput.hasNext() || myinput.hasNextInt()) { 
    userinput = myinput.nextLine(); 
    String nameofClass = userinput.toLowerCase(); 
    if (!names.contains(nameofClass)) { 
     System.out.println("I'm sorry, what was that?"); 

    } else { 
     gameplayer.setclassName(nameofClass); 
     System.out.println("Hello " + gameplayer.getPlayer() + " the "+ gameplayer.getClassName()+ ". What health do you have?"); 

     int numberofHealth = myinput.nextInt(); 
    } 
    System.out.println("Very good. Now let's get started on your adventure."); 
    gameplayer.setHealth(numberofHealth); 
    return; 
    } 
} 
+0

Kommentare sind nicht für längere Diskussionen; Diese Konversation wurde [in den Chat verschoben] (http://chat.stackoverflow.com/rooms/113943/discussion-on-answer-by-seek-addo-how-to-validate-string-from-user-input- zu-vor). –

-1

Dafür gibt es keine Schleife. Es wird einfacher sein, an eine Funktion zu delegieren, als Sie sehen werden.

List<String> acceptable = Arrays.asList("mage", "archer", "warrior"); 

System.out.print("Please enter your class. (Mage, Archer, Warrior)\n"); 

gameplayer.setclassName(promptFor(acceptable)); 


// having a function for this encapsulates the looping and the checking 
// as well as the reprompting - it also means we can leave the loop 
// with the right answer 
String promptFor(Set<String> acceptable) { 
    while(true) { // while true sucks, but you've asked for an indefinite loop 
     String next = myinput.next().toLowerCase(); 
     if (acceptable.contains(next)) { 
      return next; 
     } 

     // so it was a bad input 
     System.out.println("I'm sorry, what was that?"); 
    } // loop some more 

    return null; // unreachable code 
} 
Verwandte Themen