简体   繁体   English

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

[英]count negative and positive integer with while loop

Need to fix my program.需要修复我的程序。 Write a java program using WHILE LOOP: how many positive and negative numbers?使用 WHILE LOOP 编写 Java 程序:有多少正数和负数? User prompt the integers and ends with number 0. You will see in my output that if user enter 1 2 3 -1 -2 -3 0 My output answer: 2 positive / 3 negative.用户提示输入整数并以数字 0 结尾。您将在我的输出中看到,如果用户输入 1 2 3 -1 -2 -3 0 我的输出答案:2 正数 / 3 负数。 The first positive number is not count.第一个正数不算数。 Please let me know where is my error.请让我知道我的错误在哪里。 I cannot find it.我找不到它了。

    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)

Your code skips the first entry because you coded it that way.您的代码会跳过第一个条目,因为您是这样编码的。 Look at these lines:看看这些行:

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

You ask for the user to enter data, but then you don't do anything with it.您要求用户输入数据,但随后您什么也不做。 All your data handing takes place in the while loop.所有数据处理都在 while 循环中进行。

Replace those two lines with this:将这两行替换为:

int data = 0;

All you need to do is declare the variable you use to take input for use later on -- you don't need to use it immediately.您需要做的就是声明用于获取输入以供以后使用的变量——您不需要立即使用它。

You'll also have to modify the while condition so that on the first iteration of the loop, data with the value 0 will not automatically exit the program:您还必须修改 while 条件,以便在循环的第一次迭代中,值为 0 的数据不会自动退出程序:

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

OUTPUT:输出:

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

its seems like you are taking first input outside the while loop.似乎您正在 while 循环之外获取第一个输入。

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

and this 1 is not checked .而这个 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.

Only this values are checked.仅检查此值。

You are over riding the first input, after entering into the loop.进入循环后,您将超越第一个输入。

  1. You are reading the input and checking if input is not equal to zero or not您正在读取输入并检查输入是否不等于零
  2. After entering into the loop again you are reading the input from scanner and storing it to variable data(here the previous data is getting overridden)再次进入循环后,您正在从扫描仪读取输入并将其存储到变量数据中(这里以前的数据被覆盖)

updated Code:更新代码:

Scanner input = new Scanner(System.in);扫描仪输入 = 新扫描仪(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");
    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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM