這是一個簡單的問題的選擇,然後回答程序:Java輸入保持空白?
import java.util.Scanner;
public class Mains {
static Scanner console = new Scanner(System.in);
static Tof tof = new Tof();
static int Ievel = 0;
static int Input = 0;
static boolean GAME = true;
static boolean AT_START = true;
static boolean IN_QUESTION = false;
public static void main (String[] args) {
while (GAME) {
String InputS = "";
if (AT_START) {
System.out.println("Welcome to the game! Please select a number from 1 to 10.");
AT_START = false;
}
if (!IN_QUESTION)
Input = console.nextInt();
if (Input == -1) {
GAME = false;
console.close();
} else {
String question = tof.getQuestion(Input);
String answer = tof.getAnswer(Input);
System.out.println(question);
IN_QUESTION = true;
while (IN_QUESTION) {
InputS = console.nextLine();
if (InputS != console.nextLine()) {
if (InputS.equals(answer)) {
System.out.println("Correct!");
} else {
System.out.println("Incorrect. " + InputS + " " + answer);
}
}
}
}
}
}
}
問題:
當進入IN_QUESTION循環,並寫一個答案,它永遠是不正確的。 這是因爲InputS變量總是空的,無論如何,它設置了console.nextLine()。
爲什麼它是空的?我該如何解決?
在情況需要的其他類飛行時間質譜:http://pastebin.com/Fn5HEpL2
什麼是你的控制檯輸入?另外,你應該避免用大寫字母開始你的變量名。 Inputs應該是inputS,input應該按照代碼慣例輸入。大寫的靜態變量也應遵循正確的代碼約定。所有大寫字母通常只用於常量。 http://www.oracle.com/technetwork/java/codeconv-138413.html如果遵循代碼慣例,其他Java開發人員將更容易地解密代碼。 – JREN
我把「真」,「假」和隨機的,都是不正確的。當我打印輸入時,打印將是空的。 – user2519738