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

问题描述 投票:0回答:5

该程序的目的是接受用户输入,直到输入零,然后打印出整数信息。它还用于读取输入是否为偶数/奇数,计算总和,找到输入的最大和最小整数,计算输入的整数总数,并求平均值。当用户输入 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.");
        }

    }
}
java while-loop java.util.scanner
5个回答
2
投票

当读取第一个数字来填充大和小时,我们只需要这样做一次。并且无需使用

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
投票

您正在循环体中为

large
small
输入额外的数字。使用
++
而不是
+= 1
,我更喜欢
Integer.min
Integer.max
;分别将
small
large
初始化为非常大和非常小的值。比如,

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.");
}

0
投票

WAP 输入一个数字并检查 ut 是偶数还是奇数,直到使用无限 for 循环用户输入为零。 使用格式: 为了(;;) { }


0
投票
 Scanner input = new Scanner(System.in);

        //Take integer inputs till the user enters 0 and print the sum of all numbers (HINT: while loop)

        int n =1;
        int x = 0;

        while (n !=0){
            

            n= input.nextInt();

            x+=n;

        }
        System.out.println(x);

0
投票

包 com.dev.java;

导入java.util.Scanner;

公共类 LargestNum {

public static void main(String[] args) {
    
    Scanner in = new Scanner(System.in);
    
    System.out.println("Please enter the nuumber: ");
    int a = in.nextInt();
    
    int max = 0;
    
    while(true) {
        if(a>0) {
            if(a>max)
                max = a;
            
        }
        else {
            break;
        }
        
        a = in.nextInt();
    }
    
    System.out.println("The largest number of all is "+ max);
    
}
            

}

© www.soinside.com 2019 - 2024. All rights reserved.