Java 当从键盘输入 10 个数字时,打印 while 循环中的最大数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26341052/
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
printing the largest number from a while loop, when 10 numbers are inputted from the keyboard
提问by Shairyar Shafqat
Every time my output prints out the last number given through the Scanner as the largest number. This program needs to be modified in a way, that it scans through the numbers I input and prints out the largest number.
每次我的输出都会打印出通过扫描仪给出的最后一个数字作为最大数字。这个程序需要以某种方式修改,它扫描我输入的数字并打印出最大的数字。
P.S this is not school work, its just coding in my spare time.
PS 这不是学校作业,它只是在我的业余时间编码。
import java.util.Scanner;
public class FindTheLargestNumber {
public static void main(String[] args) {
int counter = 1;
int number;
int largest = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number: ");
number = input.nextInt();
while(counter < 10)
{
System.out.println("Enter the number: ");
number = input.nextInt();
counter++;
}
if(number > 1)
largest = number;
System.out.println("the largest number is " + largest);
}
}
CONSOLE:
安慰:
Enter the number:
123
Enter the number:
443
Enter the number:
221
Enter the number:
453
Enter the number:
553
Enter the number:
665
Enter the number:
776
Enter the number:
23
Enter the number:
12
Enter the number:
23
output:
the largest number is 23
it should clearly print out 776, but it prints the last number I input as the largest.
它应该清楚地打印出 776,但它将我输入的最后一个数字打印为最大的数字。
回答by CoderCroc
Here you are checking the condition outside the loop it only checks for the last input.As you are accepting firstvalue before the loop you should set that value as largest before the loop.(Pointed out by khelwood
)
在这里,您正在检查循环外的条件,它只检查最后一个输入。当您在循环之前接受第一个值时,您应该在循环之前将该值设置为最大。(由 指出khelwood
)
So it should be like this,
所以应该是这样的
System.out.println("Enter the number: ");
number = input.nextInt();
largest=number;
while(counter < 10){
System.out.println("Enter the number: ");
number = input.nextInt();
if(number > largest)//If largest is small, set current number as largest
largest = number;
counter++;
}
回答by Dr. Debasish Jana
You should move this within the while loop:
您应该在 while 循环中移动它:
if(number > largest)
largest = number;
回答by Prabhat Rai
You are checking just for the last accepted number if it is greater than 1 you are assigning it to the largest. But you have to make sure that you check for all the numbers and compare them to have the largest among them.
您正在检查最后一个接受的数字,如果它大于 1,您将其分配给最大的数字。但是您必须确保检查所有数字并将它们进行比较以获得其中最大的数字。
This code will work for you
这段代码对你有用
import java.util.Scanner;
public class test {
public static void main(String[] args) {
int counter = 1;
int number;
int largest = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number: ");
number = input.nextInt();
while(counter < 10)
{
System.out.println("Enter the number: ");
number = input.nextInt();
if(largest < number) {
largest = number;
}
counter++;
}
System.out.println("the largest number is " + largest);
}
}