所以我试图使 (EX)输入一些值:1 -2 -3 2 5 正数的数量是5数负数是-3 总数是3平均数是。 6 我想这样做,但是当我运行它时, 它不起作用 什么部分是错误?容易循环不工作
import java.util.*;
public class Welcome {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter an int value, the program exits if the input is 0: ");
int num = input.nextInt();
int countpos = 0;
int countneg = 0;
int totalnum = 0;
int total = 0;
double avg = 0.0;
while(num != 0){
if(num < 0)
countpos++;
else
countneg++;
total = total + num;
totalnum++;
}
System.out.print("num of pos is: " + countpos);
System.out.print("num of neg is: " + countneg);
System.out.print("total is: " + total);
System.out.print("the avg is: " + total/totalnum);
}
}
“不工作” 是不足够的问题说明。哪部分产生不需要的输出?预计什么?什么是实际?有没有编译器错误?例外?它会永远运行吗?吃冰激凌? – 2012-12-29 19:36:11
不相关,但我认为你想在你的''if''内切换循环中的标志。当他们实际上是负面的时候,你似乎在计算正数。 – Fred