Java 编写一个程序,读取未指定数量的整数,确定 pos、neg、total、average

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28553361/
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-08-11 06:24:31  来源:igfitidea点击:

Write a program that reads an unspecified number of integers, determines pos, neg, total, average

javanetbeans

提问by Chris Redfieldea

(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. Here is a sample run:

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

Enter an integer, the input ends if it is 0: 1 2 -1 3 0

输入一个整数,如果是0则输入结束:1 2 -1 3 0

The number of positives is 3

阳性数为 3

The number of negatives is 1

负数为 1

The total is 5.0

总数是 5.0

The average is 1.25

平均值为 1.25

I have 2 major issues. 1) I cannot get the loop to stop. 2) even if I did, the average comes up short. Using the example above, my average is always 1.0, not 1.25 as it should be. It's like the program is reading 5 numbers total instead of the 4 numbers that equate to 5.What is seen in the code below is all I can use: Bare basics of java...

我有两个主要问题。1)我无法让循​​环停止。2)即使我这样做了,平均值也会很短。使用上面的示例,我的平均值始终为 1.0,而不是应有的 1.25。这就像程序正在读取总共 5 个数字而不是等于 5 的 4 个数字。在下面的代码中看到的是我可以使用的所有内容:Java 的基本知识...

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;

     float average;

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

     while(number != 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.println("The average is "+ average);
     }
   }
}

采纳答案by Elliott Frisch

You need to read more numbers. You read one value before your loop. You could do something like

你需要阅读更多的数字。您在循环之前读取一个值。你可以做类似的事情

int number;
while((number = input.nextInt()) != 0) {
    total += number;
    count++;
    if(number > 0){
        positive++;
    } else if(number < 0) {
        negative++;
    }
} // <-- end loop body.
float average = total / (float) count; // <-- not integer math.
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.println("The average is " + average);

回答by bacakujt

You should just use the do while.

你应该只使用 do while。

import java.util.*;  
public class PosAndNeg   {   
public static void main(String [] args){  
Scanner input = new Scanner(System.in);  

int positive = 0, negative = 0, total = 0, count = 0;  
int number;
float average;

     System.out.println("Enter the number: ");
     do { number = input.nextInt();
     total += number;
        count++;  
           if(number > 0){
           positive++;
        }
             else if(number < 0){
            negative++;
        }
    }
     while(number != 0);

     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.println("The average is "+ average);

    }

}

}

回答by Tushar Verma

// use type conversion while evaluating average
import java.util.Scanner;
public class newClass{
public static void main(String[] args){
    int pos=0;
    int neg=0;
    int total=0;
Scanner sc= new Scanner(System.in);
System.out.println("enter the total numbers user want to enter");

int  i=sc.nextInt();
int [] num= new int[i];
System.out.println("Please enter number");
for (int j = 0; j < num.length; j++)
{   

    num[j] = sc.nextInt();
    if(num[j]>0)
        pos++;
    else
        neg++;
    total=total+num[j];

}
System.out.println(num.length);
double avg =(float)total/(num.length);
System.out.println("positive count="+pos+"negative count="+neg+" average ="+avg);
}
}