扫描仪不返回正确的结果?
问题描述:
我目前正在做一个Hi-Lo纸牌游戏作为编程任务。 我的代码来检测用户是否想猜测更高,更低或相等是低于。如果我输入'higher','lower'或'equal'(没有''),程序将不执行任何操作。 任何想法? 谢谢扫描仪不返回正确的结果?
答
您最好将插入的令牌保存到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");
}
将行读入变量并对变量进行操作。 – Makoto 2015-02-10 16:00:24
我认为这不是您在项目中使用的第一个“扫描仪”?问题是,你调用'#hasNext(String)',但从来没有'#next()',因此你从来没有真正从输入流中读取什么东西。这意味着'#hasNext(String)'总是评估用户的第一个输入。 – Tom 2015-02-10 16:05:50