メソッドを使用して浮動小数点数で配列を埋めようとしています。プログラムを実行するたびに、入力された最初の数値が取得されません。
最初のユーザー入力をキャプチャするようにコードを修正するにはどうすればよいですか?
ありがとう!
public static void main(String[] args)
{
//Read user input into the array
final int INITIAL_SIZE = 8;
double[] inputs = new double[INITIAL_SIZE];
Scanner in = new Scanner(System.in);
System.out.println("Please enter the number of credits for a course, Q to quit:");
double credits = in.nextDouble();
int currentSize = 0;
while (in.hasNextDouble())
{
if (credits <= 0)
{
System.out.println("All entries must be a positive number.");
}
else
{
// Grow the array if it has been completely filled
if (currentSize >= inputs.length)
{
inputs = Arrays.copyOf(inputs, 2 * inputs.length);
}
inputs[currentSize] = in.nextDouble();
currentSize++;
}
}
System.out.println(Arrays.toString(inputs));
}