简体   繁体   中英

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? 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. 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.

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 ((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.

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 .

      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);

// 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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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