【问题标题】:Write a program that reads an unspecified number of integers编写一个程序,读取未指定数量的整数
【发布时间】:2015-03-03 19:14:12
【问题描述】:

“计算正负数并计算数字的平均值)编写一个程序,读取未指定数量的整数,确定读取了多少个正负值,并计算输入值的总和和平均值(不是计数零)。您的程序以输入 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);
    }
}

【问题讨论】:

  • 你遇到了什么错误?
  • 为了帮助您更好地提出问题,请包括预期和实现的输出,否则它会在大海捞针中找到错误并且人们不喜欢该游戏。
  • 如果你正确缩进你的代码,你会看到你在最后有一个额外的花括号。

标签: java


【解决方案1】:

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

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

【讨论】:

    【解决方案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);,因为当你打印出一个字符串时,括号内连接的任何东西也会被转换成一个字符串,并这样打印出来

    【讨论】:

    • “如果你想要数字的平均值,你不能用整数除以整数,因为......”嗯,实际上,你可以做到这一点,如果总和足够大计数,那么得到的整数值可能足够接近某些应用程序的实际平均值。另一方面,如果总和可能相对较小,或者如果您没有足够的信息来计算总和需要多大,那么您最好使用浮点(双)算术.
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:
      // 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);
          }
      }
      

      【讨论】:

      • 它应该会给你你正在寻找的确切答案。
      【解决方案5】:

      如果您只是将System.out.printf("The average is %d ", average); 更改为System.out.printf("The average is " +average);,即如果您删除%d 并使用'+' 而不是',',那么它将对您有用,并且要在浮动中获得答案,您需要使用类型转换。即在average = (double)total / count;中添加(double)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        • 2016-01-29
        • 2017-04-24
        • 2013-02-12
        • 2019-10-02
        相关资源
        最近更新 更多