如何使用循环显示最大和最小数字? [英] What do I have to do to display the largest and the smallest number using a loop?
本文介绍了如何使用循环显示最大和最小数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道用户输入-1后如何显示最大和最小的数字.
注意:我还必须显示所有此数字的总和以及该总和的平均值.
到目前为止,这是我的代码:
public static void main(String[] args) {
// store
int cnt = 0;
int acumulator = 0;
int larger = 0;
int smaller = 0;
int number;
// inputs
System.out.println("enter the number all the number");
Scanner kb = new Scanner(System.in);
number = kb.nextInt();
// loop
while (number != -1) {
acumulator += number;
number = kb.nextInt();
cnt++;//
}
double average = (acumulator / cnt);
System.out.println(" The total of your number is=" + acumulator);
System.out.println(" The average of your number is=" + average);
}
解决方案
好像是功课,但是您可以做一个变量,然后检查输入的数字是否大于或小于保存的变量.
>
if(input > max)
max = input;
还有
if(input < min)
min = input;
I don't have any idea how to display the largest and smallest number after the user enter -1.
Note: I also have to display the sum of all of this number and the average of this sum.
Here's my code so far:
public static void main(String[] args) {
// store
int cnt = 0;
int acumulator = 0;
int larger = 0;
int smaller = 0;
int number;
// inputs
System.out.println("enter the number all the number");
Scanner kb = new Scanner(System.in);
number = kb.nextInt();
// loop
while (number != -1) {
acumulator += number;
number = kb.nextInt();
cnt++;//
}
double average = (acumulator / cnt);
System.out.println(" The total of your number is=" + acumulator);
System.out.println(" The average of your number is=" + average);
}
解决方案
Seems like schoolwork, but what you could do is making a var and checking in your while if the input number is higher or lower then the saved var.
if(input > max)
max = input;
And
if(input < min)
min = input;
这篇关于如何使用循环显示最大和最小数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文