我對編碼很陌生,希望你們能幫助我。 我有一個nextDouble沒有等待print.ln收集數據的問題。 隨着我的啓動輸入,我必須輸入兩個數字來移動到下一行,並要求輸入第二個數字,因爲它沒有等待。 到了這一點,它也決定要求第三個數字,並接受該輸入,但可以毫無問題地要求第三個和第四個數字。 重複時,它也發生在循環內部。掃描儀不等待NewLine收集循環輸入
我知道使用nextDouble並不意味着它會「等待」換行符,而且我知道nextNumbertype沒有換行符標誌,但 在得到一個double之後加入「input.nextLine」似乎沒有工作。
我知道我可以將所有輸入作爲字符串並稍後進行轉換,但這是一項家庭作業,我的教授似乎想要一種非常特定的方式。
對於什麼是錯的或我可以嘗試的想法?
這裏是輸出:
Enter x1 or <crtl + z> to quit
1
4
Please input the x2 number:
Please input the y1 number:
1
Please input the y2 number:
5
Distance is 5.000000
Enter x1 or <crtl + z> to quit
下面是實際的代碼:
import java.util.Scanner;
public class distanceFinder {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double x1 = 0;
System.out.println("Enter x1 or <crtl + z> to quit");
x1 = input.nextDouble();
while(input.hasNext())
{
System.out.println("Please input the x2 number:");
double x2 = input.nextDouble();
System.out.println("Please input the y1 number:");
double y1 = input.nextDouble();
System.out.println("Please input the y2 number:");
double y2 = input.nextDouble();
double x3 = 0;
double y3 = 0;
double xy1 = 0;
x3 = Math.pow((x2 - x1), 2);
y3 = Math.pow((y2 - y1), 2);
xy1 = x3 + y3;
double distance = Math.sqrt(xy1);
System.out.printf("Distance is %.6f %n%n%n", distance);
System.out.println("Enter x1 or <crtl + z> to quit");
x1 = input.nextDouble();
}
input.close();
}
}
問題是與'hasNext'按照javadcos *此方法可以在等待要掃描的輸入框* –