需要确定程序中的正数,负数和零数,并分别添加所有正数和负数

时间:2018-11-04 13:09:53

标签: java

我需要确定程序中的正数,负数和零数,并分别添加所有正数和负数。我正在使用while循环(可以使用do-while),因为不允许for循环和数组。急需您的帮助。这是我的代码。该代码应允许在确定之前输入10个数字。

public class Mix22 {
    public static void main(String[] args) {
        Scanner ety = new Scanner(System.in);

        int count=0;
        int positive=0;
        int negative =0;
        int num=0;


        System.out.println("Enter a number: ");
        num = ety.nextInt();

        while(num!=10){

            if(num<0)
                negative++;

            if (num>0)
                positive++;

            System.out.println("Enter a number: ");
            num = ety.nextInt();

        }

        System.out.println("Negative numbers in the program: " + negative);
        System.out.println("Positive numbers in the program: " + positive);
    }
}

1 个答案:

答案 0 :(得分:0)

是否要运行循环10次的问题?您有一个count变量,您没有使用它。循环应类似于:

int count=0;
while (count != 10) {
    ...
    ++count;
}

为此,通常使用for循环(如果允许):

for (int count=0; count<10; ++count) {
    ...
}
相关问题