Determine Two Numbers which is the largest or Both Numbers are Equal in Java
In this Java program, the application will determine two input numbers which is the largest, or both the numbers input are equal.
This is the same solution of Deitel & Deitel exercise 2.17 in Chapter 2.
Write an application that ask the user to enter two integers, obtains the two numbers of the user and display the larger number followed by the words "is larger" in an information message dialog. If the number are equal, print the message "These numbers are equal".
public class LargerOrEqual {
public static void main(String args[]){
//Declare varables
String fNumber, sNumber, result;
int numb1, numb2;
//Read the user inputs
fNumber = JOptionPane.showInputDialog("Enter first number: ");
sNumber = JOptionPane.showInputDialog("Enter second number: ");
//Conver input string to integer values
numb1 = Integer.parseInt(fNumber);
numb2 = Integer.parseInt(sNumber);
result = "";
if (numb1 > numb2)
result = numb1 + " is larger";
else if (numb1 <= 0)
result = "Please enter a positive number!";
if (numb1 < numb2)
result = numb2 + " is larger";
else if (numb2 <= 0)
result = "Please enter a positive number!";
if (numb1 == numb2)
result = "These numbers are equal";
//Prints and display the values
JOptionPane.showMessageDialog(null, result
,"Larger or Equal Program", JOptionPane.INFORMATION_MESSAGE);
}
}
Here is the output. In this case assumes user input the numbers "5", "6"
This is the same solution of Deitel & Deitel exercise 2.17 in Chapter 2.
Write an application that ask the user to enter two integers, obtains the two numbers of the user and display the larger number followed by the words "is larger" in an information message dialog. If the number are equal, print the message "These numbers are equal".
public class LargerOrEqual {
public static void main(String args[]){
//Declare varables
String fNumber, sNumber, result;
int numb1, numb2;
//Read the user inputs
fNumber = JOptionPane.showInputDialog("Enter first number: ");
sNumber = JOptionPane.showInputDialog("Enter second number: ");
//Conver input string to integer values
numb1 = Integer.parseInt(fNumber);
numb2 = Integer.parseInt(sNumber);
result = "";
if (numb1 > numb2)
result = numb1 + " is larger";
else if (numb1 <= 0)
result = "Please enter a positive number!";
if (numb1 < numb2)
result = numb2 + " is larger";
else if (numb2 <= 0)
result = "Please enter a positive number!";
if (numb1 == numb2)
result = "These numbers are equal";
//Prints and display the values
JOptionPane.showMessageDialog(null, result
,"Larger or Equal Program", JOptionPane.INFORMATION_MESSAGE);
}
}
Here is the output. In this case assumes user input the numbers "5", "6"
Comments
Post a Comment