在這個片段中,我正在做三種不同類型的變量並添加它們,然後再打印。Java掃描器不能接受字符串後點擊進入
public static void main(String[] args) {
int i = 4;
double d = 4.0;
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
int firstVariable = 0;
double secondVariable = 0.0;
String theString = "";
firstVariable = scan.nextInt();
secondVariable = scan.nextDouble();
theString = scan.nextLine();
System.out.println(firstVariable+i);
System.out.println(secondVariable+d);
System.out.println(s+""+theString);
}
我儘快提供firstVariable
擊球輸入輸入,然後提供輸入爲secondVariable
,現在爲我打進入theString被捕獲值(我知道這應該捕獲它)。
編輯:在這種情況下,我應該如何提供輸入到theString
而不是與空間?
我曾嘗試這樣的事情,
while(scan.hasNext())
theString = scan.nextLine();
但它也不能工作。
@Tom不,它不重複,請檢查編輯部分。這些重複鏈接的問題沒有編輯部分的答案。我的意思是處理這兩種情況,即在空間和新線路之後提供輸入。 – piechuckerr