|
Re: Not sure what to do with if in my situation [message #1715090 is a reply to message #1715078] |
Thu, 19 November 2015 02:38  |
Eclipse User |
|
|
|
Kyle,
This is a basic programming question best asked on something like
Stackoverflow. Here the question should be about problem related to
using Eclipse specifically.
That being said, if you run this in the debugger---Eclipse has a very
good one---you can step through the code and see why this is happening.
I would start by formatting your code so it's less confusing. Then
you'll see your first block has an else that shows a message and then
the second condition also shows a message.
On 19/11/2015 5:09 AM, Kyle Weekly wrote:
> Hello,
> I cannot seem to have two if's without both appearing. I just want one to appear for each situation. So basically its text boxes and if entered age is above 45 it should say (Name) is very old. But if entered age if less than 45 it says (Name) is a (Age) year old. It ends up showing both dialogues. Pls help the noob
>
> Program/Code (There is also a screen shot):
>
>
>
>
> package first;
>
> import javax.swing.JOptionPane;
>
> import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
>
> public class Main
> {
> public static void main(String[] args)
> {
> String firstName;
> String lastName;
>
>
>
>
>
>
> JOptionPane.showMessageDialog(null, "Welcome");
>
> firstName = JOptionPane.showInputDialog("Enter Your First Name");
> lastName = JOptionPane.showInputDialog("Enter Your Last Name");
>
> String s = JOptionPane.showInputDialog("Enter Your Age");
> int age = Integer.parseInt(s);
>
> if(age < 45){
> JOptionPane.showMessageDialog(null, firstName + " " + lastName + " is" + " a " + age + " year old.");}else{
> JOptionPane.showMessageDialog(null, firstName + " " + lastName + " is " + age + " year old.");}
>
> if(age > 45){
> JOptionPane.showMessageDialog(null, firstName + " " + lastName + " is" + " very old.");}
> }
> }
>
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.07296 seconds