0

メソッドを使用して浮動小数点数で配列を埋めようとしています。プログラムを実行するたびに、入力された最初の数値が取得されません。

最初のユーザー入力をキャプチャするようにコードを修正するにはどうすればよいですか?

ありがとう!

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));
 }
4

2 に答える 2

1

問題は

最初のユーザー エントリを保存しなかったため、表示されません

  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();

ユーザーから値を取得しましたが、保存していませんinputs

creditユーザーから値を取得して保存creditしたい場合は、次のinputsようにする必要があります。

 double credits = in.nextDouble();
 inputs[0] = credits ;
    int currentSize = 1;   
于 2013-09-06T04:01:01.213 に答える