我正在尝试使用一种方法用浮点数填充数组。每次我运行我的程序时,它都不会捕获输入的第一个数字。
如何更正我的代码以便捕获第一个用户输入?
谢谢!
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));
}