扫描仪没有返回正确的结果?

Her*_*ire 0 java

我正在制作一个Hi-Lo纸牌游戏作为编程任务.我检测用户是想要猜测更高,更低还是更低的代码如下.

DeckOfCards deck = new DeckOfCards(); 
PlayingCard card = deck.deal(); 
PlayingCard next = deck.deal(); 
System.out.println("Welcome to the High Low Card Game.\n" +
                   "You're current card is: "+card.toPictograph() + 
                   ". Will the Next card be higher/lower/equal "+
                   "to the current card?\nType Quit to exit.");
Scanner inputScanner = new Scanner(System.in);
if(inputScanner.hasNext("higher")){

        System.out.println("Congradulations you won");
        wins++;
}
 else if (inputScanner.hasNext("lower"))
 {
     System.out.println("You suck");
 }
 else if (inputScanner.hasNext("equal"))
 {
     System.out.println("You suck");
 }
Run Code Online (Sandbox Code Playgroud)

如果我键入'higher','lower'或'equal'(没有''),程序什么都不做.有任何想法吗?谢谢

Ara*_*mey 6

您最好将插入的标记保存到String变量中,然后将其与您想要的任何内容进行比较.

.next()只会保存你的第一个字.如果要保存整行(包括空格),请使用.nextLine()

DeckOfCards deck = new DeckOfCards(); 
PlayingCard card = deck.deal(); 
PlayingCard next = deck.deal(); 
System.out.println("Welcome to the High Low Card Game.\nYou're current card is: "+card.toPictograph()+". Will the Next card be higher/lower/equal to the current card?\nType Quit to exit.");
Scanner inputScanner = new Scanner(System.in);
String s = inputScanner.next();
if(s.equalsIgnoreCase("higher"))
{
    System.out.println("Congradulations you won");
    wins++;
}
else if (s.equalsIgnoreCase("lower"))
{
    System.out.println("You suck");
}
else if (s.equalsIgnoreCase("equal"))
{
    System.out.println("You suck");
}
Run Code Online (Sandbox Code Playgroud)