netbeans 使用while循环计算负整数和正整数

q5lcpyga  于 2022-11-10  发布在  其他
关注(0)|答案(4)|浏览(161)

需要修复我的程序。用WHILE LOOP写一个java程序:有多少个正数和负数?用户提示整数,并以数字0结尾。您将在我的输出中看到,如果用户输入1 2 3 -1 -2 -3 0我的输出答案:2正/ 3负。第一个正数不算数。请告诉我我的错误在哪里。我找不到。

Scanner input= new Scanner(System.in);

    //int data;
    int count = 0;
    int negative=0;
    int positive =0;

    System.out.print("Enter an integer (Program ends if enter 0): ");
    int data = input.nextInt();

    while (data !=0) {

    System.out.print("Enter an integer (Program ends if enter 0): ");
    data=input.nextInt();
    //count++;
    if (data < 0){
    negative++;
    }else if (data > 0){
    positive++;
    }
    count++;
  }
    System.out.println(positive + " positive numbers");
    System.out.println(negative + " negative numbers");

 }
}

output:
run:
Enter an integer (Program ends if enter 0): 1
Enter an integer (Program ends if enter 0): 2
Enter an integer (Program ends if enter 0): 3
Enter an integer (Program ends if enter 0): -1
Enter an integer (Program ends if enter 0): -2
Enter an integer (Program ends if enter 0): -3
Enter an integer (Program ends if enter 0): 0
2 positive numbers
3 negative numbers
BUILD SUCCESSFUL (total time: 11 seconds)
6qfn3psc

6qfn3psc1#

这看起来就像是在while循环之外获取第一个输入。

System.out.print("Enter an integer (Program ends if enter 0): ");
int data = input.nextInt(); // the first input 1 store in here

并且不检查该1。

while (data !=0) {
 System.out.print("Enter an integer (Program ends if enter 0): ");
data=input.nextInt();// 2,3,-1,-2,-3, 0 are inside the loop.

仅检查这些值。

wribegjk

wribegjk2#

你的代码跳过了第一个条目,因为你是这样编写的。看看下面这几行:

System.out.print("Enter an integer (Program ends if enter 0): ");
int data = input.nextInt();

你要求用户输入数据,但是你什么也不做,所有的数据处理都在while循环中进行。
将这两行替换为:

int data = 0;

您所需要做的就是声明用于获取输入以供以后使用的变量--您不需要立即使用它。
您还必须修改while条件,以便在循环的第一次迭代中,值为0的数据不会自动退出程序:

while ((data !=0) || (count == 0 && data == 0)) {

输出:

Enter an integer (Program ends if enter 0): 1
Enter an integer (Program ends if enter 0): 2
Enter an integer (Program ends if enter 0): 3
Enter an integer (Program ends if enter 0): -1
Enter an integer (Program ends if enter 0): -2
Enter an integer (Program ends if enter 0): -3
Enter an integer (Program ends if enter 0): 0
3 positive numbers
3 negative numbers
5kgi1eie

5kgi1eie3#

在进入循环后,您将覆盖第一个输入。
1.您正在阅读输入并检查输入是否不等于零
1.再次进入循环后,您将从scanner阅读输入,并将其存储到变量数据中(此处,先前的数据将被覆盖)
更新的代码:
扫描器输入=新的扫描器(System.in);

// int data;
int count = 0;
int negative = 0;
int positive = 0;

System.out.print("Enter an integer (Program ends if enter 0): ");
int data = input.nextInt();

while (data != 0) {
    // count++;
    if (data < 0) {
        negative++;
    } else if (data > 0) {
        positive++;
    }
    count++;

    System.out.print("Enter an integer (Program ends if enter 0): ");
    data = input.nextInt();
}
System.out.println(positive + " positive numbers");
System.out.println(negative + " negative numbers");
o0lyfsai

o0lyfsai4#

package Loops;
    import java.util.Scanner;
    import java.util.SortedMap;
    public class Question12 {
        public static void main(String[] args) {
            Scanner sc= new Scanner(System.in);
            System.out.println("How many numbers you want to enter?\n");
            int x= sc.nextInt();
            int [] data= new int[x];
            for(int i=0;i<data.length;i++){
                System.out.println("Enter the number "+(i+1));
                data[i]=sc.nextInt();
            }
            int count=0;
            int negative=0;
            int zero=0;
            for(int i: data){
                if(i>0){
                    count++;
                }else if(i<0){
                    negative++;
                }else if(i==0){
                    zero++;
                }}
            System.out.println("The positive number count is: "+count);
            System.out.println("The negative number count is: "+negative);
            System.out.println("The zero number count is: "+zero);
        }
    }

相关问题