Write a Java program (Largest.java) that prompts the user to enter integer value
ID: 3652061 • Letter: W
Question
Write a Java program (Largest.java) that prompts the user to enter integer values until the user types 9999, and then prints the two largest numbers (in any order) in the input. How many numbers the user will enter is unknown in advance. However, you may assume that the user will enter at least two numbers before entering the sentinel value.This is my code so far:
import java.util.*;
public class Largest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
largest(input);
}
public static void largest(Scanner input) {
System.out.println("Please enter a number (9999 to quit):");
int number = input.nextInt();
while (number != 9999) {
System.out.println("Please enter a number (9999 to quit):");
number = input.nextInt();
}
}
}
I need help on to to produce the output of the 2 largest numbers besides the sentinel value (9999)
Explanation / Answer
import java.util.*; public class Largest { public static void main(String[] args) { Scanner input = new Scanner(System.in); largest(input); } public static void largest(Scanner input) { System.out.println("Please enter a number (9999 to quit):"); int number ; int max1 = -9999; int max2 = -9999; while (1) { System.out.println("Please enter a number (9999 to quit):"); number = input.nextInt(); if(number == 9999) break; if( number > max1 && number > max2) max1 = number; else if ( number > max2 && numberRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.