所以我沒有意識到程序正在運行,只是空白,因為java讀取下來了,系統(tǒng)在我看到答案之前一直在等我輸入答案。謝謝@wdc的幫助。原來的我目前正在練習(xí)Java,遇到了一個已解決的問題,但我不明白為什么,當(dāng)我有這樣的程序時,程序如何運行:import java.util.Scanner;public class Practice { public static void main(String[] args) { Scanner in = new Scanner(System.in); int number1 = (int)(System.currentTimeMillis() % 10); int number2 = (int)(System.currentTimeMillis() / 10 % 10); System.out.print("What is " + number1 + " + " + number2 + "?"); int answer = in.nextInt(); System.out.println(number1 + " + " + number2 + " = " + answer + " is " + (number1 + number2 == answer)); }}但是當(dāng)我有這樣的時候是行不通的:import java.util.Scanner;public class Practice { public static void main(String[] args) { Scanner in = new Scanner(System.in); int number1 = (int)(System.currentTimeMillis() % 10); int number2 = (int)(System.currentTimeMillis() / 10 % 10); int answer = in.nextInt(); System.out.print("What is " + number1 + " + " + number2 + "?"); System.out.println(number1 + " + " + number2 + " = " + answer + " is " + (number1 + number2 == answer)); } }我想知道,這樣我以后就可以避免這個問題。
2 回答

人到中年有點甜
TA貢獻1895條經(jīng)驗 獲得超7個贊
如果我正確理解了您的問題,則不確定這兩種情況下的輸出為何不同?
請注意,java按照代碼中出現(xiàn)的順序執(zhí)行語句。所以,
當(dāng)你把int answer = in.nextInt();
在之前System.out.print("What is " + number1 + " + " + number2 + "?");
你的程序是等待用戶輸入,你看不到任何東西印在屏幕上。如果您在控制臺中輸入內(nèi)容,然后按Enter鍵,程序?qū)⒗^續(xù)執(zhí)行,您將看到其余的輸出。
但是,如果您int answer = in.nextInt();
在打印語句之后移動,則將執(zhí)行第一個打印語句,并且您會在控制臺中看到一些輸出。

30秒到達戰(zhàn)場
TA貢獻1828條經(jīng)驗 獲得超6個贊
好吧,說它不起作用并不是真的正確,因為確實如此。問題是在第二個代碼段中,在問題的控制臺打印之前,您具有阻止方法“ in.nextInt()”,因此僅在遇到問題后才需要讓他回答。
添加回答
舉報
0/150
提交
取消