编写一个读取未指定数量的整数的程序

时间:2015-03-03 19:14:13

标签: java

“计算正数和负数并计算数字的平均值”编写一个读取未指定数量的整数的程序,确定已读取的正负值的数量,并计算输入值的总和和平均值(不是计数零。。程序以输入0结束。将平均值显示为浮点数。“

我不知道我做错了什么

import java.util.Scanner;

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

        int positive = 0, negative = 0, total = 0, count = 0;

        double average;

        System.out.println("Enter the number: ");
        int number;

        while ((number = input.nextInt()) != 0) {
            total += number;
            count++;
            if (number > 0) {
                positive++;
            } else if (number < 0) {
                negative++;
            }
        }
        average = total / count;
        System.out.println("The number of positives is " + positive);
        System.out.println("The number of negatives is " + negative);
        System.out.println("The total is " + total);
        System.out.printf("The average is %d ", average);
    }
}

5 个答案:

答案 0 :(得分:3)

首先:它应该是average = (double)total / count;,因为int / int比得到一个整数。

第二:System.out.println("The average is " + average);System.out.printf("The average is %f ", average);

答案 1 :(得分:2)

如果你想要数字的平均值,你不能将整数total除以整数count,因为结果将是一个整数,它不考虑小数点。

import java.util.Scanner;

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

        int positive = 0, negative = 0, total = 0, count = 0;

        double average;

        System.out.println("Enter the number: ");
        int number;

        while ((number = input.nextInt()) != 0) {
            total += number;
            count++;
            if (number > 0) {
                positive++;
            } else if (number < 0) {
                negative++;
            }
        }
        average = (double) total / count;
        System.out.println("The number of positives is " + positive);
        System.out.println("The number of negatives is " + negative);
        System.out.println("The total is " + total);
        System.out.printf("The average is: " + average);
    }
}

此外,您不必在行System.out.printf("The average is %d", average);

中使用%d

您可以编写System.out.printf("The average is: " + average);,因为当您打印出一个字符串时,括号内连接的任何内容也将转换为字符串,并打印出来

答案 2 :(得分:0)

只需将int变量乘以1.0即可将其转换为浮点变量     average=1.0*total/count; 这应该做。 您可以使用以下语句来显示值 System.out.println("The average of numbers is "+average);

答案 3 :(得分:0)

// Scanner is in java.util package
import java.util.Scanner;
class CountPandN
{
    public static void main(String args[])
    {
        // create a Scanner object
        Scanner input = new Scanner(System.in);
        // prompt user to enter numbers
        System.out.println("Enter + and - numbers");
        System.out.println("Enter 0 when you're finished");
        // initialize the variables
        int n, countP, countN, count;
        n = input.nextInt();
        countP = 0;
        countN = 0;
        count = 0;
        int sum = n;
        float average = (float) sum / 2;

        while (n != 0)
        {
            n = input.nextInt();
        count++;

        if(n >= 0)
            countP++;
        if (n < 0)
            countN++;
        }
        System.out.println("Total positive " + countP);
        System.out.println("Total negative " + countN);
        System.out.println("Total numbers " + count);
        System.out.println("Total average " + average);
    }
}

答案 4 :(得分:0)

如果您仅用System.out.printf("The average is %d ", average);来更改System.out.printf("The average is " +average);,即如果您删除%d并使用'+'而不是',',那么它将为您工作,并且得到浮动的答案,您需要使用类型转换。即在(double)

中添加average = (double)total / count;