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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28840042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:15:25  来源:igfitidea点击:

Write a program that reads an unspecified number of integers

java

提问by Andrew Poley

"Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers , determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number."

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

I don't know what I did wrong

我不知道我做错了什么

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

回答by pL4Gu33

First: it should be average = (double)total / count;because int / int than you get an integer.

第一:应该是average = (double)total / count;因为int/int比你得到的整数。

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

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

回答by freddiev4

If you want the average of numbers, you cannot divide an integer totalby an integer countbecause the result will be an integer, which does not account for decimal points.

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

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

Also, you don't have to use %d in your line System.out.printf("The average is %d", average);

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

You can write System.out.printf("The average is: " + average);because when you print out a String, anything concatenated within the parentheses will also be converted to a String, and printed out as such

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

回答by Piyush Vishwakarma

Simply multiply your int variable by 1.0 to convert it into floating point variable average=1.0*total/count;This should do. And you can use following statement to display the value System.out.println("The average of numbers is "+average);

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

回答by Piyush Vishwakarma

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

回答by Sajid S Shaikh

if you simply change System.out.printf("The average is %d ", average);with System.out.printf("The average is " +average);i.e. if you remove%dand use '+'instead ','then it will work for you, and also to get answer in float you need to use typecasting. i.e. add (double)in average = (double)total / count;

如果你简单地改变System.out.printf("The average is %d ", average);System.out.printf("The average is " +average);也就是说,如果你删除%d和使用'+',而不是','那么它会为你工作,并且也取得浮法答案,你需要使用类型转换。即加(double)average = (double)total / count;