简体   繁体   English

当从键盘输入 10 个数字时,打印 while 循环中的最大数字

[英]printing the largest number from a while loop, when 10 numbers are inputted from the keyboard

Every time my output prints out the last number given through the Scanner as the largest number.每次我的 output 打印出通过扫描器给出的最后一个数字作为最大数字。 This program needs to be modified in a way, that it scans through the numbers I input and prints out the largest number.这个程序需要在某种程度上进行修改,它会扫描我输入的数字并打印出最大的数字。

PS 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,但它打印出我最后输入的最大数字。

You are checking just for the last accepted number if it is greater than 1 you are assigning it to the largest. 您正在检查的是最后接受的数字,如果该数字大于1,则将其分配给最大的数字。 But you have to make sure that you check for all the numbers and compare them to have the largest among them. 但是你必须确保检查所有数字并将它们进行比较以获得最大的数字。

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

Here you are checking the condition outside the loop it only checks for the last input. 在这里,您正在检查循环外的条件,该条件仅检查最后一个输入。 As you are accepting first value 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++;
}

You should move this within the while loop: 你应该在while循环中移动它:

if(number > largest)
    largest = number;
    int counter = 1;
    int number;
    int largest = 0;
    Scanner scanner = new Scanner(System.in);
    for (;counter <= 10; counter++) {
        System.out.println("ENTER " + counter + " NUMBER");
        number = scanner.nextInt();


        if (number > largest  ) {
            largest = number;

        }

    }
    System.out.println("THE LARGEST NUMBER FOUND SO FAR IS: " + largest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM