Results 1 to 6 of 6
  1. #1
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default again, newbe question..

    To keep it short, i wrote a program that asks user to enter 3 variables and outputs them in an order from low value to high.

    My problem is with the output. If i have 3 of the same values it causes the program to output the same answer twice. From what i have looked at it, everything is correct and i can not find a solution. Here is the code.



    Java Code:
    /**************************************************************************************
    ** 																					 **
    ** This program asks user to input three names and the time in minutes it took them	 **
    ** to finish the race. Output should arrange the names from frist place to last. 	 **
    **																					 **
    **************************************************************************************/
    
    
    
    import java.util.Scanner;
    
    public class RunningTheRace
    {
    	public static void main(String[] args)
    	{
    		
    		String name1;
    		String name2;
    		String name3;
    		double time1;
    		double time2;
    		double time3;
    		
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.print("Enter name #1: ");		// prompt user to enter name
    		name1 = keyboard.nextLine();
    		
    		System.out.print("Enter " + name1 + "'s time: ");
    		time1 = keyboard.nextDouble();
    		System.out.println("");						// empty line
    		
    		keyboard.nextLine();						// eat a line
    		
    		System.out.print("Enter name #2: ");
    		name2 = keyboard.nextLine();
    		
    		System.out.print("Enter " + name2 + "'s time: ");
    		time2 = keyboard.nextDouble();
    		
    		System.out.println("");
    		
    		keyboard.nextLine();
    		
    		System.out.print("Enter name #3: ");
    		name3 = keyboard.nextLine();
    		
    		System.out.print("Enter " + name3 + "'s time: ");
    		time3 = keyboard.nextDouble();
    		
    		System.out.println("");
    		
    		if (time1 <= time2 && time2 != time3)						// if time1 is less than or equal to follow through, else go to next statement
    		{
    			if (time1 <= time3)
    			{
    				System.out.println(name1);
    				
    				if(time2 <= time3)
    				{
    					System.out.println(name2);
    					System.out.println(name3);
    				}
    				
    				else if (time3 <= time2)
    				{
    					System.out.println(name3);
    					System.out.println(name2);
    				}
    			}
    		}
    		
    		if (time2 <= time1 && time1 != time3 && time3 != time1)
    		{
    			if (time2 <= time3)
    			{
    				System.out.println(name2);
    				
    				if(time1 <= time3)
    				{
    					System.out.println(name1);
    					System.out.println(name3);
    				}
    				
    				else if (time3 <= time1)
    				{
    					System.out.println(name3);
    					System.out.println(name1);
    				}
    			}
    		}
    		
    		if (time3 <= time2 && time2 != time1 && time1 != time3)
    		{
    			if (time3 <= time1)
    			{
    				System.out.println(name3);
    				
    				if(time1 <= time2)
    				{
    					System.out.println(name1);
    					System.out.println(name2);
    				}
    				
    				else if (time2 <= time1)
    				{
    					System.out.println(name2);
    					System.out.println(name1);
    				}
    			}
    		}
    		
    	}
    }

    also here is what i get when i set variable 1 as a greater number than variable 2 and variable 3 and keep variable 2 and 3 the same. If I have different values for all of the numbers the program works flawlessly.





    any help is appreciated!
    Last edited by glina126; 02-22-2011 at 03:40 AM. Reason: the code was copied wrong! sorry!
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You have 3 separate if statements. If you are getting 2 sets of output then 2 of those 3 if statements are true. Which means your logic is incorrect as only one should ever be true.

  3. #3
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You have 3 separate if statements. If you are getting 2 sets of output then 2 of those 3 if statements are true. Which means your logic is incorrect as only one should ever be true.
    hmm now i see that, i thought it was not reading something correctly. you are right, my logic failed at this instance. i think i need to call it a night and head to bed. thanks for enlightening me! ;)
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I'll give you a hint, tidy up your if statements.
    Java Code:
    if(var1 < var2 && var 1 < var3) {
        // we know var1 must be smallest
        // now what?
    } else if(etc) {

  5. #5
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    I'll give you a hint, tidy up your if statements.
    Java Code:
    if(var1 < var2 && var 1 < var3) {
        // we know var1 must be smallest
        // now what?
    } else if(etc) {

    ok that is true, i could write it in one statement. But you do not include =, meaning that in the example i gave the program will just quit. I want it to be able to work with values of the same amount.
    I just woke up, time to tidy up and then i could figure out how to tidy up the if statements hehe :)
    i will post the code if i figure out the solution.

    just a question, why dont these booleans kick in and fix it?

    Java Code:
    if (time1 <= time2 && time2 != time3 && time3 != time1)
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You don't need to use equals. Less than will do the trick. I'll expand my code above.
    Java Code:
    if(var1 < var2 && var 1 < var3) {
        // we know var1 must be smallest
        // now what?
    } else if(var2 < var3) {
        // now we know var2 must be the smallest
    Lets say the three inputs are 2, 2 and 3. The first if statement fails as 2(var1) is not less than 2(var2). The second if statement is true as 2(var2) is less than 3(var3). Inside that second if statement you need another if statement (i'll let you work it out). What will happen is that var2 gets printed then var1 and then var3: 2, 2, 3. Since var1 and var2 have the same value they both get printed before var3 but to the human eye you cannot tell whether var1 or var2 gets printed first.

Similar Threads

  1. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  2. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  3. Please help me PLEASe with this question
    By javanew in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 12:45 PM
  4. RMI question
    By alvandrood in forum New To Java
    Replies: 1
    Last Post: 09-14-2009, 12:36 PM
  5. question
    By ayoood in forum Java Software
    Replies: 6
    Last Post: 07-07-2008, 01:32 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
  •