简体   繁体   中英

Breaking out of a program and calculating min/max values from user input

I'm trying to use user input and calculate the sum, amount of numbers entered, min and max values using an array of the user input (not arraylist) and average of the sum. The program should quit when the user enters 0. I'm not entirely sure how to exit it so that if 0 is entered, it would just exit the while loop, other than using System.exit(0). I tried using a break statement here but does not work.

Also, when adding the sum, I initialized the input variable twice (one outside the loop and one inside), which causes the sum not to add up in the end.

If anyone can advise me on how to fix it or just tips i'd appreciate it.


        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a list of integers: ");
        int input = scanner.nextInt();
        int sum = 0;
        int numCount = 0;
        double average = 0.0;
        

        while (input != 0) {
            input = scanner.nextInt();
            
            if(input == 0) {
                System.out.println("Exiting");
                break;
            }
            
            // sum
            sum += input;
            numCount++;
            
            // min
            
            // max
            
            // avg
            average = sum / numCount;

        }

        System.out.println("Sum is: " + sum);
        System.out.println("Number count: " + numCount);
        System.out.println("Lowest value is: ");
        System.out.println("Lowest value is: ");
        System.out.println("Highest value is: ");
        System.out.println("Average of numbers: " + average);

        }```

I'm trying to use user input and calculate the sum, amount of numbers entered, min and max values using an array of the user input (not arraylist) and average of the sum. The program should quit when the user enters 0. I'm not entirely sure how to exit it so that if 0 is entered, it would just exit the while loop, other than using System.exit(0). I tried using a break statement here but does not work.

Also, when adding the sum, I initialized the input variable twice (one outside the loop and one inside), which causes the sum not to add up in the end.

If anyone can advise me on how to fix it or just tips i'd appreciate it.


        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a list of integers: ");
        int input = scanner.nextInt();
        int sum = 0;
        int numCount = 0;
        double average = 0.0;
        

        while (input != 0) {
            input = scanner.nextInt();
            
            if(input == 0) {
                System.out.println("Exiting");
                break;
            }
            
            // sum
            sum += input;
            numCount++;
            
            // min
            
            // max
            
            // avg
            average = sum / numCount;

        }

        System.out.println("Sum is: " + sum);
        System.out.println("Number count: " + numCount);
        System.out.println("Lowest value is: ");
        System.out.println("Lowest value is: ");
        System.out.println("Highest value is: ");
        System.out.println("Average of numbers: " + average);

        }```

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