0

所以我试图让(EX)输入一些值:1 -2 -3 2 5 num of positive num is 5 num of neg num is -3 total is 3 avg is .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 );

}

}

4

1 回答 1

2

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

 while(num != 0){

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

        total = total + num;
        totalnum++;

        num = input.nextInt();
    }
于 2012-12-29T19:33:31.247 回答