[英]The Java Scanner is not taking my String Input
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
這是我的代碼。 它編譯正確,沒有語法錯誤,但我不明白為什么我的字符串輸入沒有被采用。 一旦我給出 integer 輸入和雙輸入,output 就會出現:
輸入
42
309.23
OUTPUT
細繩:
雙倍:309.23
詮釋:42
請注意,對於完成單線輸入1 1.1 done
您將獲得 output:
String: done
Double: 1.1
Int: 1
在 output 中done
此操作之前,有一個空格字符提示您掃描儀一次使用一個令牌。 查看例如scan.nextInt和scan.nextLine的文檔
scan.nextDouble()
或scan.nextInt()
沒有完成該行,因此當scan.nextLine()
變得簡單時,您可以說它消耗了剩余的空白行,因此 String 什么也不打印。 要消耗剩余的行,請將scan.nextLine()
放在String s = scan.nextLine();
之前
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.