Hello everyone. I am required to write a simple code that reads 10 numbers and displays the largest and last number entered. I thought i would be able to do this easily but for some reason the numbers wont get assigned to the variables like i thought.

import java.util.Scanner;
public class LargestNumber {

	public static void main(String[] args) {
			
			int largest=0;
			int number=0;
			int counter=1;
			
		Scanner input=new Scanner (System.in);
		
		System.out.println("Please enter a number: ");
		number=input.nextInt();
		
		number=largest;
		counter++;
		
		while (counter <= 10){		
			
			System.out.println("Enter another number: ");
			number=input.nextInt();
				if (number>largest)
					number=largest;
				
				counter++;

		}
		System.out.println("Last number entered was: "+ number+".");
		System.out.println("The largest number entered was: "+ largest+".");
	}

}

thats what i made, which i thought would work fine...but then the output says the largest and number variables are equal to 0. I set them to 0 at the top, but how come they arnt assigned the new numbers when the while loop executes?

Recommended Answers

All 2 Replies

Maybe you want to reverse line 23?

commented: always helpful +1

ah i knew it had to be something small and stupid...thanks again.

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.