I am writing a program that uses a while loop to determine the largest number input so far. I have to input 10 numbers in whole. At the end of the loop, it should output the largest number.

import java.util.Scanner;

public class Largest
{
	public static void main(String[] args)
	{
		int counter = 1;
		int number;
		int largest;
		int number2;
		
		Scanner input = new Scanner(System.in);
		
		while (counter < 10)
		{
			System.out.println("Enter a number");
			number = input.nextInt();
                  
                      // ???
                 }

Recommended Answers

All 11 Replies

At any given point there is one largest number. Either the new number isn't larger than the previous largest number, or it is. What do you do in the first case? What do you do in the second?

Well I edited it to make it to where it could get the largest number out of each iteration of the loop, but it wants the largest number in all, like out of the 10 numbers. I can't think of a way to do this without an array.

while (counter < 10)
		{
			System.out.println("Enter a number");
			number = input.nextInt();
			
			System.out.println("Enter another number");
			number2 = input.nextInt();
			
			if (number > number2)
				largest = number;
			else
				largest = number2;
				
				counter++;
				
				System.out.println(largest);
			}

Why do yo need a second number? You want to see if the new number is bigger than the largest number entered in previous iterations of the loop. Where is that value stored?

Idk, it just tells me to use at least these 3 variables

a) counter: A counter to count to 10 (i.e., to keep track of how many numbers have been
input and to determine when all 10 numbers have been processed).
b) number: The integer most recently input by the user.
c) largest: The largest number found so far.

And that's all you need. You don't need to input a second number in the loop.

Suppose you're in the middle of the loop, you know you've looked at five numbers and the largest so far is, let's say, 76. Now we start the sixth time through, and the input is 98. What happens?

Well then I guess that 98 becomes the largest then.

Right. And if the next time you get, say 87?

Then the largest number is still 98, but the variable keeps getting set each time it goes through. Idk how to implement that in an if statement.

If the new number is larger, you set largest. Otherwise, you do nothing.

if (number > largest) 
  largest = number;

import java.util.Scanner;

public class Largest

public static void main(String[] args)

    int counter = 1;
    int number;
    int largest=0


    Scanner input = new Scanner(System.in);

    while (counter < 10)

        System.out.println("Enter a number");
        number = input.nextInt();

If(number>largest)
largest=number;
Else
largest=largest;
counter++;

System.out.println("the largest number is "+largest);

Hi shamsudeen98, welcome to DaniWeb

You have posted some code to a topic that has been dead for 9 years!

The code itself is full of errors, so why did you post it?
If you posted it as a solution to this topic then you should have tested it first to avoid embarrasing yourself
If you posted it because you would like some help with your code then
(a) start you own new topic and
(b) explain exectly what help you need

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.