'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. 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.
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.
Solution 1:[1]
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.
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);
}
}
Solution 2:[2]
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
)
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++;
}
Solution 3:[3]
You should move this within the while loop:
if(number > largest)
largest = number;
Solution 4:[4]
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Prabhat Rai |
Solution 2 | |
Solution 3 | khelwood |
Solution 4 | Koleaje olayinka |