该程序的目的是接受用户输入,直到输入零,然后打印出整数信息。它还用于读取输入是否为偶数/奇数,计算总和,找到输入的最大和最小整数,计算输入的整数总数,并求平均值。当用户输入 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.");
}
}
}
当读取第一个数字来填充大和小时,我们只需要这样做一次。并且无需使用
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;
}
您正在循环体中为
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.");
}
WAP 输入一个数字并检查 ut 是偶数还是奇数,直到使用无限 for 循环用户输入为零。 使用格式: 为了(;;) { }
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);
包 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);
}
}