2016-06-05 2 views
-1

안녕하세요. 사용자가 미리 정의한 것 이외의 다른 내용을 입력하면 코드를 입력하라는 루프를 작성하려고합니다. 나는 특정 단어 나 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; 
      } 



      } 

} 
+0

내 질문에 내용이 다를 수도 있지만 그 근처에는 스레드가 없습니다. – BabyC0d3eR

답변

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

설명은 확장 토론이 아닙니다. 이 대화는 [채팅으로 이동되었습니다] (http://chat.stackoverflow.com/rooms/113943/discussion-on-answer-by-seek-addo-how-to-validate-string-from-user-input- 투 - 프리). –

-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 
} 
관련 문제