读取用户输入直到输入 0 的 Java 程序,还会进行一些计算(偶数/几率、平均值等)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48482222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:05:18  来源:igfitidea点击:

Java program that reads user input until 0 is entered, also makes a few calculations (evens/odds, average, etc.)

javawhile-loopjava.util.scanner

提问by Sharon Nystrom

This program is meant to take user input until a zero is entered, and then print out information on the integers. It's also meant to read if the input in even/odd, calculate the sum, find the largest and smallest entered integers, tally the total integers entered, and find the average. It won't stop when the user enters 0, and it won't print the "No data entered" line if there's no input. It also isn't figuring even numbers properly.

该程序旨在接受用户输入,直到输入零,然后打印出有关整数的信息。它还意味着读取输入是否为偶数/奇数,计算总和,找到输入的最大和最小整数,计算输入的总整数,并找到平均值。当用户输入 0 时它不会停止,如果没有输入,它不会打印“未输入数据”行。它也没有正确计算偶数。

import java.util.Scanner;

public class Lab4 {

 public static void main(String[] args) {

    int counter = 0;
    double even = 0;
    double odd = 0;
    double sum = 0;
    int input = 0;
    int large = 0;
    int small = 0;
    double average;

    System.out.print("Enter a series of values (0 to quit): ");
    Scanner in = new Scanner(System.in);

    while ((input = in.nextInt()) != 0) {

        small = in.nextInt();
        large = in.nextInt();

        if (input != 0)
            sum = input + sum;
            counter++;

        if (input > large)
            large = input;

        if (input < small)
            small = input;

        if (input % 2 == 0)
            even = even + 1;
        else
            odd = odd + 1;

        }

    if (counter > 0) {

        average = sum / counter;

        System.out.println("The smallest integer is: " + small);
        System.out.println("The largest integer is: " + large);
        System.out.println("Total number of integers entered is " + counter);
        System.out.println("Total even numbers entered is " + even);
        System.out.println("Total odd numbers entered is " + odd);
        System.out.println("The average value is: " + average);
    } else {

            System.out.println("No data was entered.");
        }

    }
}

采纳答案by devilfart

When reading the first numbers to populate large and small we only need to do that once. And without re-reading the numbers with in.nextInt()because that will eat up the next inputs that are entered, which was probably causing the not terminating at zero error.

当读取第一个数字来填充大小时,我们只需要这样做一次。并且无需重新读取数字,in.nextInt()因为这会耗尽输入的下一个输入,这可能导致不以零错误终止。

while ((input = in.nextInt()) != 0) {

        if (counter == 0)
          small = large = input;

        if (input != 0)
            sum = input + sum;
            counter++;

        if (input > large)
            large = input;

        if (input < small)
            small = input;

        if (input % 2 == 0)
            even = even + 1;
        else
            odd = odd + 1;

        }

回答by Elliott Frisch

You are inputting extra numbers in the loop body for largeand small. Use ++instead of += 1, I would prefer Integer.minand Integer.max; initialize smalland largeto something very large and very small respectively. Something like,

您在循环体中为largeand输入了额外的数字small。使用++代替+= 1,我更喜欢Integer.minInteger.max;分别初始化smalllarge到非常大和非常小的东西。就像是,

double even = 0, odd = 0, sum = 0;
int counter = 0, input = 0, large = Integer.MIN_VALUE, small = Integer.MAX_VALUE;
System.out.print("Enter a series of values (0 to quit): ");
Scanner in = new Scanner(System.in);
while ((input = in.nextInt()) != 0) {
    small = Integer.min(small, input);
    large = Integer.max(large, input);
    sum += input;
    counter++;
    if (input % 2 == 0) {
        even++;
    } else {
        odd++;
    }
}

if (counter > 0) {
    double average = sum / counter;
    System.out.println("The smallest integer is: " + small);
    System.out.println("The largest integer is: " + large);
    System.out.println("Total number of integers entered is " + counter);
    System.out.println("Total even numbers entered is " + even);
    System.out.println("Total odd numbers entered is " + odd);
    System.out.println("The average value is: " + average);
} else {
    System.out.println("No data was entered.");
}