容易循环不工作

时间:2012-12-29 19:32:33

标签: java for-loop while-loop do-while

所以我试图制作 (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 );

}

}

1 个答案:

答案 0 :(得分:2)

你必须在循环中做num = input.nextInt();

 while(num != 0){

        if(num < 0)
            countpos++;
        else
            countneg++;

        total = total + num;
        totalnum++;

        num = input.nextInt();
    }