(计算正数和负数并计算数字的平均值)写 读取未指定数量的整数的程序确定了多少 已读取正值和负值,并计算总值和平均值 输入值(不包括零)。您的程序以输入0结束。显示 平均值为浮点数。这是一个示例运行:
输入一个整数,输入结束,如果它是0:1 2 -1 3 0
积极数为3
否定数为1
总数为5.0
平均值为1.25
我有两个主要问题。 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);
}
}
}
答案 0 :(得分:2)
您需要阅读更多数字。你在循环之前读取了一个值。你可以做点什么
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);
答案 1 :(得分:0)
你应该只使用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);
}
}
答案 2 :(得分:0)
// 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);
}
}
答案 3 :(得分:-3)
import java.util.Scanner;
public class NewClass{
public static void main(String[] args) {
int N=0,p=0,t=0;
System.out.println("enter
integers");
Scanner obj=new Scanner
(System.in);
int a=obj.nextInt();
while (a!=0){
t+=a;
if (a<0){
N++;
}
else if (a>0){
p++;
}
int b=obj.nextInt();
a=b;
}
System.out.println("the
number of negative
numbers is\t\t"+N);
System.out.println("the
number of positive number
is\t\t"+p);
float l=N+p;
float average=(float) t/l;
System.out.println("the total
is\t\t"+t);
System.out.println("the
average is\t\t"+average);
}
}