+ 1

Can anyone help me with my Java assignment?

I have a Java assignment in my coding class where it is asking me to write a while loop where a user can repeatedly enter numbers until they enter '-1'. I did this easily but my problem is after the while loop is complete it wants me to print the largest integer they entered. I have no idea how to find the largest number they input during a while loop. It gave a tip as well. Tip: Use an if statement. Example below: Scanner scn = new Scanner(System.in); int score = scn.nextInt(); while (score != -1) { score = scn.nextInt(); } System.out.println("The largest number is: " + largestnumber);

31st Oct 2022, 3:24 PM
Thomas Young
2 Answers
+ 3
Assign initial value 0 to largestnumber . If the current entered number is geaterthan previous value, change largest to current input. Otherwise don't. You will end having largestnumber in variable.
31st Oct 2022, 3:36 PM
Jayakrishna 🇼🇳
0
Use a variable to store the largest number, e.g. largestnumber in your code fragment. Every time you read a new score, compare it to the value in largestnumber. If score is greater that largestnumber, then overwrite largestnumber with score. This way, after the final number has been read, largestnumber will hold the largest of all numbers entered during execution of the program.
31st Oct 2022, 8:43 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar