使用for循环只读取5个数字并取平均值。 JAVA

时间:2014-10-14 07:10:55

标签: java for-loop average

我真的遇到了这个程序的问题。我已经搜索了这个网站和其他一些网站,但还没有找到解决方案。看起来我只是在寻求帮助,但我真的被卡住了。我要创建一个程序,从用户读取5个数字并平均这些数字。我对Java的了解程度是Scanner类和for循环,但还没有使用while循环。这是编写得非常糟糕的代码:

public class Average5
{
   public static void main(String args[])
{
   Scanner sc = new Scanner(System.in);
   int num;

   System.out.print("Please enter a number: ");
   num = sc.nextInt();

   for(int num1 = 0; num1 <= num; num1++)
   {
        System.out.print("Please enter a number: ");
        num = sc.nextInt();
   }

老实说,我不知道该怎么做。或多或少的自学。

5 个答案:

答案 0 :(得分:2)

public class Average5 {

    public static void main(String args[]) {

        int numlenngth = 5;
        double total = 0;
        Scanner sc = new Scanner(System.in);

        for (int num1 = 0; num1 < numlenngth; num1++) {
            System.out.print("Please enter a number: ");
            total += sc.nextInt();
        }

        System.out.println("Average : " + (total / numlenngth));

    }
}

输出&gt;&gt;

Please enter a number: 1
Please enter a number: 1
Please enter a number: 1
Please enter a number: 1
Please enter a number: 2
Average : 1.2

答案 1 :(得分:0)

要计算精确平均值,您需要使用double(sum)而不是int,并且每次使用用户值添加(sum)。

public class Average5
{
   public static void main(String args[]){
   Scanner sc = new Scanner(System.in);
   System.out.print("Please enter how many numbers you want to enter for calculating average: ");
   int totalCount = sc.nextInt();
   double sum=0;

   for(int i= 0; i<totalCount; i++)
   {
        System.out.print("Please enter a number: ");
        sum += sc.nextDouble();
   }

   System.out.println("Average of number is"+(sum/totalCount));

}

答案 2 :(得分:0)

您可以在此处使用数组来存储数字:

public class Average5 {
    private static final int MAX = 5;

    public static void main(String[] args) {
        int[] numbers = new int[MAX];
        Scanner sc = new Scanner(System.in);

        for(int i = 0; i < MAX; i++) {
            System.out.println("Please type the number");
            numbers[i] = sc.nextInt();
        }

        float average = getAverage(numbers);
        System.out.println("The average of the five numbers: " + average);
    }

    public static float getAverage(int[] arg0) {
        int sum = 0;
        float Ret = 0.0f;

        if(arg0 != null) {
            for(int i = 0; i < MAX; i++) {
            sum += arg0[i];

            Ret = sum / MAX;
        }

        return Ret;
    }
}

这不是解决这个问题的唯一方法。

答案 3 :(得分:0)

这与其他人发布的几乎相同,但仅限于输入5个数字,它会处理最终部门中可能产生的浮点数:

import java.util.Scanner;

public class Average5 {

    private static final int TOTAL = 5;

    public static void main(final String[] args) {
        final Scanner scanner = new Scanner(System.in);
        double sum = 0;

        System.out.format("You have to enter %d numbers now.\n", TOTAL);

        for (int cnt = 1; cnt <= TOTAL; cnt++) {
            System.out.format("Please enter number %d of %d: ", cnt, TOTAL);
            sum += scanner.nextInt();
        }

        System.out.format("The average is %f.\n", sum / TOTAL);
        scanner.close();
    }

}

答案 4 :(得分:-1)

int num = 0;
float total = 0f;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a number: ");
num = sc.nextInt();

for(int num1 = 0; num1 <= num; num1++)
   {
        System.out.print("Please enter a number: ");
        total += sc.nextInt();
   }

System.out.println("Average : "+(total/num));