gpt4 book ai didi

java - 扫描仪未返回正确结果?

转载 作者:行者123 更新时间:2023-12-01 07:57:15 26 4
gpt4 key购买 nike

我目前正在制作一款 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");
}

如果我输入“更高”、“更低”或“等于”(不带“”),程序将不会执行任何操作。有任何想法吗?谢谢

最佳答案

您最好将插入的标记保存到 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");
}

关于java - 扫描仪未返回正确结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28436113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com