亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當我有int答案= in.nextInt()時怎么辦?在第8行中什么都沒有出現

當我有int答案= in.nextInt()時怎么辦?在第8行中什么都沒有出現

守候你守候我 2021-05-06 18:34:54
所以我沒有意識到程序正在運行,只是空白,因為java讀取下來了,系統在我看到答案之前一直在等我輸入答案。謝謝@wdc的幫助。原來的我目前正在練習Java,遇到了一個已解決的問題,但我不明白為什么,當我有這樣的程序時,程序如何運行: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));    }}但是當我有這樣的時候是行不通的: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條經驗 獲得超7個贊

如果我正確理解了您的問題,則不確定這兩種情況下的輸出為何不同?

請注意,java按照代碼中出現的順序執行語句。所以,

當你把int answer = in.nextInt();在之前System.out.print("What is " +  number1 + " + " + number2 + "?");你的程序是等待用戶輸入,你看不到任何東西印在屏幕上。如果您在控制臺中輸入內容,然后按Enter鍵,程序將繼續執行,您將看到其余的輸出。

但是,如果您int answer = in.nextInt();在打印語句之后移動,則將執行第一個打印語句,并且您會在控制臺中看到一些輸出。


查看完整回答
反對 回復 2021-05-19
?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

好吧,說它不起作用并不是真的正確,因為確實如此。問題是在第二個代碼段中,在問題的控制臺打印之前,您具有阻止方法“ in.nextInt()”,因此僅在遇到問題后才需要讓他回答。


查看完整回答
反對 回復 2021-05-19
  • 2 回答
  • 0 關注
  • 457 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號