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

时间:2018-01-28 01:15:45

标签: java while-loop java.util.scanner

此程序用于输入用户输入,直到输入零,然后打印出有关整数的信息。它还意味着读取偶数/奇数输入,计算总和,找到最大和最小输入的整数,计算输入的总整数,并找出平均值。当用户输入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.");
        }

    }
}

2 个答案:

答案 0 :(得分:1)

当读取第一个数字以填充大小时我们只需要这样做一次。并且没有使用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;

        }

答案 1 :(得分:0)

您正在largesmall的循环体中输入额外的数字。使用++代替+= 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.");
}