连续循环直到满足条件

时间:2016-09-27 17:22:43

标签: java loops sum average

所以我必须在eclipse中为我的类创建一个java项目。赋值是创建一个程序,允许用户在程序中输入整数,直到输入某个整数(42)。输入整数(42)后,程序将 1 。平均输入的所有数字。的 2 即可。显示输入的最小和最大数量。 3 即可。输入的总数。这些必须在不计算(42)的情况下计算。这就是我到目前为止所拥有的。我可以从用户那里获得输入,一旦他们输入42,程序就会停止并显示总数但包括42。我不确定如何添加"计数"对于输入,所以我可以使用计数显示输入的总数以及从总和除以计数给我的平均值。我感谢任何帮助。

package assignment6;

import java.util.Scanner;

public class assignment6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Scanner
        Scanner input = new Scanner(System.in);

        //Enter integer
        System.out.println("Enter a integer ");
        int data = input.nextInt();

        //Accept integers until 42 is entered
        int sum = 42;
        while (data != 42) {
            sum += data;

            System.out.println("Enter another integer ");
            data = input.nextInt();
        }
        //Display sum of numbers entered
        System.out.println("The sum of all numbers entered is: " + sum);

        //Display Average of numbers entered
        System.out.println("Average of all numbers entered is:" + ( sum/ ));

    }
}

4 个答案:

答案 0 :(得分:1)

检查以下代码。

package stackoverflow;

import java.util.Scanner;

public class LoopTest {

public static void main(String[] args) 
{
    // TODO Auto-generated method stub

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter number");

    int input  = 0;
    double sum = 0;
    double avg = 0;
    int count = 0;

    //Infinite Loop
    while (true)
    {   
        //Accept Number
        input  = scanner.nextInt();

        //Check Entered Number
        if (input  != 42)
        {
            sum = sum + input ;
            count = count + 1;
            avg = sum/count;
        }
        else
            break;
    }

    System.out.println("Numbers Entered : " + count);
    System.out.println("Sum of Numbers : " + sum);
    System.out.println("Average of Numbers : " + avg);
}

}

答案 1 :(得分:0)

如果sum设置为0,则可以计算42。

此外,使用while(true)永远循环,break;结束循环。执行中断判断后没有任何内容,所以

int count = 1;
while(true) {
   sum += data;

   System.out.println("Enter another integer ");
   data = input.nextInt();
   count++;
   if(data == 42) {
       break;
   }
}

答案 2 :(得分:0)

import java.util.Scanner;

public class assignment6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Scanner
        Scanner input = new Scanner(System.in);

        //Enter integer
        System.out.println("Enter a integer ");
        int data = input.nextInt();
        int sum   = 0 ;
        int count = 0 ;
        //Accept integers until 42 is entered

        while (data != 42) {
            sum += data;
            count += 1;

            System.out.println("Enter another integer "); 
            data = input.nextInt();
        }
        //Display sum of numbers entered
        System.out.println("The sum of all numbers entered is: " + sum);

        //Display Average of numbers entered
        System.out.println("Average of all numbers entered is:" + ( sum/count ));

    }
} 

答案 3 :(得分:-1)

要添加计数,您只需要做一个变量

    int count = 0;

例如,每次循环重复时只需添加一个