简体   繁体   中英

How can I make my user aware of how many numbers they input whether it was negative or positive

So far I have this program which is already pretty close to what I want anyways. But am trying to figure out a way after the user input all his/her number he can know if he input 3 negative numbers and 4 posistive numbers

so let say he inputs -7,-8,-3,2,3,4,2 it says you have input 3 negative numbers and 4 postive numbers

import java.util.*;
public class Testing2 {
    public static void main(String[] args) {

        int numbers;
        System.out.println("Input seven numbers");  
        for (int i = 1; i <8; i++){
            Scanner Nums = new Scanner(System.in);

            numbers = Nums.nextInt ();
            if (numbers < 0){
                System.out.println("You have " + numbers + " numbers that are negative");
            } else {
                System.out.println("You have "+ numbers + " numbers that are postive");
            }
        }
    }
}

and what does it mean Resource leak: nums is never closed

I am using eclipse and this what shows up. Anyone know why?S

Try this:

import java.util.*;
public class Testing2 {
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Don't need to close as System.in
        int numNegative = 0, numPositive = 0;
        System.out.println("Input seven numbers");  
        for (int i = 1; i < 8; i++){

            int number = scanner.nextInt();
            if (number >= 0){ // Is the number positive or 0
                numPositive++;
            } else { // Otherwise
                numNegative++;
            }
        }
        System.out.println("You have " + numPositive + " numbers that are positive");
        System.out.println("You have " + numNegative + " numbers that are negative");
    }
}

One way to do it is to use an array to keep track of your seven numbers, and two variables for keeping track of the positive or negative numbers(eg pos_numbers and neg_numbers),

This way, each time you receive input from the user, the if else statement tests whether the number is positive or negative, and if its positive then it will increment the pos_numbers variable by one, and if its negative then it will increment the neg_numbers variable by one. After the user is finished entering the numbers, the program will display the values of pos_numbers and neg_numbers to show how many positive numbers and negative numbers were input by the user respectively.

Also, it's a good idea to put the line where you create the Scanner object before the for loop, since this only needs to be done once instead of multiple times.

Here is the code:

    import java.util.*;
    class Testing2 {
        public static void main(String[] args) {

            int[] numbers = new int[7];
            int pos_numbers = 0;
            int neg_numbers = 0;
            Scanner Nums = new Scanner(System.in);

            System.out.println("Input seven numbers");  
            for (int i = 0; i <7; i++) {
                numbers[i] = Nums.nextInt();

                if (numbers[i] < 0) neg_numbers++;
                else pos_numbers++;
            }

            System.out.println("You have " + neg_numbers + " numbers that are negative");
            System.out.println("You have "+ pos_numbers + " numbers that are postive");
        }
    }

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