2017-02-25 72 views
-4

當我輸入「little code」作爲輸入(在最後一行)時,我期望輸出「我寫了一小段代碼」(在最後一行),但輸出是「我寫了一個「 這是我的代碼:`級聯在Java中沒有像預期的那樣工作

String s = "I wrote a "; 
int i = 4; 
double j = 7.0; 
Scanner scan = new Scanner(System.in); 
String c; int a; double b; 
a = scan.nextInt(); 
b = scan.nextDouble(); 
c = scan.nextLine(); 
System.out.println(i + a); 
System.out.println(j + b); 
System.out.println(s + c); 
scan.close(); 
+1

您確定您確實在控制檯中輸入了某些東西,而這是導致您的問題的代碼? **你需要按enter/make一個換行符**才能使nextLine工作。 – Moira

+2

對我來說看起來不錯。 – DevilsHnd

+0

你使用在線編譯器嗎? –

回答

0

這段代碼是對我工作的罰款:

String s = "I wrote a "; 
int i = 4; 
double j = 7.0; 
Scanner scan = new Scanner(System.in); 
String c; int a; double b; 
a = scan.nextInt(); 
b = scan.nextDouble(); 
scan.nextLine(); 
c = scan.nextLine(); 
System.out.println(i + a); 
System.out.println(j + b); 
System.out.println(s + c); 
scan.close(); 

只是缺少scan.nextLine()後,b = scan.nextDouble ();

相關問題