0

やあみんな私は数平均化プログラムの作成に取り組んでいて、ユーザーが文字「y」を入力して再度実行して別の計算を実行できるようにするこの関数を挿入したかったのですが、私のプログラムは終了メッセージを表示します(私は使用していますEclipse) 最初の計算の後、ユーザーが入力できるようにしたいのですが。これが私を困惑させるソースコードの部分です:

public static void main(String[] args)
{
    Scanner input= new Scanner(System.in);

    System.out.print("This is an averaging program. How many numbers would you like to average together?");
    int total=input.nextInt();
    int i;
    float sum=0;
    for(i=0; i<total; i++)
    {
        System.out.print("Enter your numbers: ");
        sum += input.nextFloat();
    }
    String y;

    System.out.print("Your average is: " + sum/total + "\n");
    System.out.print("Would you like to do another computation? Type y for yes, or something else for no.");
    y=input.nextLine();
4

1 に答える 1

3

これを試して:

変化する

sum += input.nextFloat();

sum += input.nextFloat();
input.nextLine();

int total=input.nextInt();

int total=input.nextInt();
input.nextLine();

説明。\n数字フォームを読んだ後、手動で改行文字を読む必要がありますScanner

もちろん、do while繰り返し実行するには、プログラムの関連部分もループに追加する必要がありますが、おそらくそれはご存知でしょう。

于 2013-02-14T02:46:39.743 に答える