Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default conditional statement (part 2) please checxk my codes!

    that only display the temp n5 and not the lowest smallest integer...
    I was required to use that library and conditional statement...
    please help me... thanks!




    Java Code:
    import javax.swing.*;
    public class santosCase09
    {
    	public static void main(String args[])
    	{
    		int n1,n2,n3,n4,n5; String temp="";
    		n1=Integer.parseInt(JOptionPane.showInputDialog(null,"First integer: ","Enter a number",JOptionPane.PLAIN_MESSAGE));
    		n2=Integer.parseInt(JOptionPane.showInputDialog(null,"Second integer: ","Enter a number",JOptionPane.PLAIN_MESSAGE));
    		n3=Integer.parseInt(JOptionPane.showInputDialog(null,"Third integer: ","Enter a number",JOptionPane.PLAIN_MESSAGE));
    		n4=Integer.parseInt(JOptionPane.showInputDialog(null,"Fourth integer: ","Enter a number",JOptionPane.PLAIN_MESSAGE));
    		n5=Integer.parseInt(JOptionPane.showInputDialog(null,"Fifth integer: ","Enter a number",JOptionPane.PLAIN_MESSAGE));
    		temp="The smallest integer is "+n1;
    		temp="The smallest integer is "+n2;
    		temp="The smallest integer is "+n3;
    		temp="The smallest integer is "+n4;
            temp="The smallest integer is "+n5;
    		JOptionPane.showMessageDialog(null,temp   ,"Smallest",JOptionPane.PLAIN_MESSAGE);
    		if (n1<n2){
    			    if (n1<n2)
    			    	temp=""+n1;
    				else if(n1>n2)
    				    temp=""+n2;
    				else if (n1>n3)
    					temp=""+n3;
    				else if (n1>n4)
    					temp=""+n4;
    				else if (n1>n5)
    					temp=""+n5;
    		}
    }
    }
    Last edited by Fubarable; 07-11-2010 at 11:42 PM. Reason: quote block changed to a code block
    Heart is the only broken thing that still works.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderator edit: quote block changed to a code block. OP, please click on my signature link on how to use code tags.

    Much luck!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,900
    Rep Power
    25

    Default

    Could you please explain what the problem is?

    The JOptionPane shows the message its been given.
    Do you want it to show something else?
    Why do you show the message BEFORE your search code?

  4. #4
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Could you please explain what the problem is?

    The JOptionPane shows the message its been given.
    Do you want it to show something else?
    Why do you show the message BEFORE your search code?

    I should find the smallest integer among five integers that I entered.
    Heart is the only broken thing that still works.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,900
    Rep Power
    25

    Default

    Ok, that is a definition of what the program is to do.
    What is your design/algorithm for doing it? The steps you must take in the program to find the smallest integer.
    Are you allowed to use an array? Without an array this becomes a very ugly program to write.

  6. #6
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Ok, that is a definition of what the program is to do.
    What is your design/algorithm for doing it? The steps you must take in the program to find the smallest integer.
    Are you allowed to use an array? Without an array this becomes a very ugly program to write.

    I was asked to use Conditional statement only for this. :(
    Heart is the only broken thing that still works.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,900
    Rep Power
    25

    Default

    Conditional only!? With 5 values to compare against each other.
    That sounds like a lot of comparisons.

    Before you try writing the program, you need to design it.
    Do you know how to use a flowchart to design your program?
    Or perhaps pseudo code would work.
    You need some tool to work thru the possible combinations until you see the pattern.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,900
    Rep Power
    25

    Default

    How about this: Can you test the numbers as they are read in?
    Doing the testing after they all read in is not the way to solve this.

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    you can do this by:
    Java Code:
     1. Declare an integer that will hold the smallest value.
        [b]int nSmallest[/b]
     2. In the first user input pass the value to nSmallest.
        [b]nSmallest = <user input>[/b]
     3. In second to fifth input compare nSmallest to the user input. If user input is less than nSmallest then nSmallest is equals to user input.
        [b]ENTER <2nd,3rd,4th,5th> integer: _____
            if(<user inptut> < nSmallest)
           {    nSmallest = <user inptut>
           }[/b]
     4. Inform user what is the smallest integer.
        [b]"Smallest integer is" nSmallest[/b]
    Good Luck

Similar Threads

  1. conditional statement
    By blindfolded916 in forum New To Java
    Replies: 12
    Last Post: 07-11-2010, 10:09 AM
  2. JSP Help - Conditional Meta Tags
    By jakavan in forum New To Java
    Replies: 0
    Last Post: 12-08-2008, 07:34 PM
  3. how to do conditional looping?
    By chennee72 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-09-2008, 01:38 PM
  4. JFrame Conditional Close
    By hemanthjava in forum AWT / Swing
    Replies: 10
    Last Post: 06-29-2008, 08:49 AM
  5. Algorithm statement in my Java codes
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-24-2008, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •