我目前正在制作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");
}
如果我键入'higher','lower'或'equal'(没有''),程序什么都不做。 有任何想法吗? 谢谢
答案 0 :(得分: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");
}