2

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.

3
  • 1
    Your test for largest is outside the loop. Commented Oct 13, 2014 at 13:24
  • 1
    if(number > 1) should be if(number > largest) Commented Oct 13, 2014 at 13:25
  • thanks a lot friends, it helped out a ton. Commented Oct 13, 2014 at 13:30

4 Answers 4

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

appreciate the effort.
1

You should move this within the while loop:

if(number > largest)
    largest = number;

Comments

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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.