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?