Write a Java application program called Largest.java that inputs a series of 10
ID: 3530934 • Letter: W
Question
Write a Java application program called Largest.java that inputs a series of 10 single-digit numbers and determines and prints the largest of the numbers. Except main() method, no other user-defined method is required. Your program should use at least the following three variables
1. counter: A counter to count to 10 (that is, to keep track of how many numbers have been input and to determine when all 10 numbers have been processed);
2. number: The current digit input to the program; and
3. largest: The largest number found so far.
Belowis what I have so far (I hope that this posts correctly, for some reason I have been having trouble getting it to display right) I need help with an error message for anything input not 0-9, also I kind of want to hide the counter. Any help would be greatly appreciated:
import javax.swing.JOptionPane;
public class Largest
{
// the main method
public static void main(String[] args)
{
int largest = 0;
int counter = 1;
int number = 0;
while ( counter <= 10)
{
number = Integer.parseInt(JOptionPane.showInputDialog("Enter single digit number " +
counter + ": "));
if (number >= 1 && number <= 9)
{
counter++;
if (number > largest)
largest = number;
}
}
JOptionPane.showMessageDialog(null, "The largest number is " + largest);
}
}
Explanation / Answer
please rate
//your code was running fine for integer between 0-9,but you want to hide the counter and also want to calculate the larget that can be anything input not 0-9,then your modified version of code is
import javax.swing.JOptionPane;
public class Largest
{
// the main method
public static void main(String[] args)
{
int largest = 0;
int counter = 1;
int number = 0;
while ( counter <= 10)
{
number = Integer.parseInt(JOptionPane.showInputDialog("Enter single digit number "));
counter++;
if (number > largest)
largest = number;
}
JOptionPane.showMessageDialog(null, "The largest number is " + largest);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.