如何验证从用户输入到预定义名称的字符串

时间:2016-06-05 22:26:25

标签: java

大家好我试图在我的代码中编写一个循环,如果他们输入的内容不是我预定义的内容,则会提示用户。我对用户输入有些熟悉,这些用户输入不是特定的单词或int,但不确定用户有三个选择可供选择。

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;
            }



            }

}

2 个答案:

答案 0 :(得分:-1)

没有必要循环。如您所见,它将更容易委托给一个函数。

 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
 }

答案 1 :(得分:-1)

尝试这个,它应该解决剩下的问题。

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;
   }
}