Results 1 to 6 of 6
Thread: again, newbe question..
- 02-22-2011, 03:34 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
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)
- 02-22-2011, 03:42 AM #2
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.
- 02-22-2011, 03:50 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)
- 02-22-2011, 04:05 AM #4
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) {
- 02-22-2011, 04:00 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
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)
- 02-22-2011, 10:23 PM #6
You don't need to use equals. Less than will do the trick. I'll expand my code above.
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.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
Similar Threads
-
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01-14-2011, 12:05 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Please help me PLEASe with this question
By javanew in forum New To JavaReplies: 2Last Post: 03-26-2010, 12:45 PM -
RMI question
By alvandrood in forum New To JavaReplies: 1Last Post: 09-14-2009, 12:36 PM -
question
By ayoood in forum Java SoftwareReplies: 6Last Post: 07-07-2008, 01:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks