2015-02-10 79 views
0

我目前正在做一個Hi-Lo紙牌遊戲作爲編程任務。 我的代碼來檢測用戶是否想猜測更高,更低或相等是低於。如果我輸入'higher','lower'或'equal'(沒有''),程序將不執行任何操作。 任何想法? 謝謝掃描儀不返回正確的結果?

+0

將行讀入變量並對變量進行操作。 – Makoto 2015-02-10 16:00:24

+0

我認爲這不是您在項目中使用的第一個「掃描儀」?問題是,你調用'#hasNext(String)',但從來沒有'#next()',因此你從來沒有真正從輸入流中讀取什麼東西。這意味着'#hasNext(String)'總是評估用戶的第一個輸入。 – Tom 2015-02-10 16:05:50

回答

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"); 
}