Okay for my homework my professor said to do ask the user for 3 names and what time they finished the race and put that in from first place - second place to third place.
this is what i got so far but i need help with the outcome of the place they are in.
Code:import javax.swing.JOptionPane; //Needed for Scanner Object /** * @(#)Runners.java * * Runners application * * @author * @version 1.00 2013/3/3 */ public class Race { public static void main(String[] args) { //Declare variables int time1; int time2; int time3; String input; //Ask user for names and time String runner1; runner1 = JOptionPane.showInputDialog("Whats the first runners name?"); input = JOptionPane.showInputDialog("What time did he finish?"); time1 = Integer.parseInt(input); String runner2; runner2 = JOptionPane.showInputDialog("Whats the second runners name?"); input = JOptionPane.showInputDialog("What time did he finish?"); time2 = Integer.parseInt(input); String runner3; runner3 = JOptionPane.showInputDialog("Whats the third runners name?"); input = JOptionPane.showInputDialog("What time did he finish?"); time3 = Integer.parseInt(input); //Outcome if (time1<time2<time3) JOptionPane.showMessageDialog(null, "The winner is " + runner1 + " Followed by " + runner2 + "," + runner3); else if (time3<time2<time1) JOptionPane.showMessageDialog(null, "The winner is " + runner3 + " Followed by " + runner2 + "," + runner1); else if (time2<time1<time3) JOptionPane.showMessageDialog(null, "The winner is " + runner2 + " Followed by " + runner1 + "," + runner3); else if (time1<time3<time2) JOptionPane.showMessageDialog(null, "The winner is " + runner1 + " Followed by " + runner3 + "," + runner2); else if (time2<time3<time1) JOptionPane.showMessageDialog(null, "The winner is " + runner2 + " Followed by " + runner3 + "," + runner1); else if (time3<time1<time2) JOptionPane.showMessageDialog(null, "The winner is " + runner3 + " Followed by " + runner1 + "," + runner2); System.exit(0); } }
Results 1 to 22 of 22
Thread: Program help
- 05 Mar. 2013 08:47pm #1
- Age
- 97
- Join Date
- Nov. 2009
- Location
- In the computer
- Posts
- 11,186
- Reputation
- 1029
- LCash
- 10.34
- Awards
Program help
- 05 Mar. 2013 09:12pm #2
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
Code:if (time1<time2 && time2<time3) JOptionPane.showMessageDialog(null, "The winner is " + runner1 + " Followed by " + runner2 + "," + runner3); else if (time3<time2 && time2<time1) JOptionPane.showMessageDialog(null, "The winner is " + runner3 + " Followed by " + runner2 + "," + runner1); else if (time2<time1 && time1<time3) JOptionPane.showMessageDialog(null, "The winner is " + runner2 + " Followed by " + runner1 + "," + runner3); else if (time1<time3 && time3<time2) JOptionPane.showMessageDialog(null, "The winner is " + runner1 + " Followed by " + runner3 + "," + runner2); else if (time2<time3 && time3<time1) JOptionPane.showMessageDialog(null, "The winner is " + runner2 + " Followed by " + runner3 + "," + runner1); else if (time3<time1 && time1<time2) JOptionPane.showMessageDialog(null, "The winner is " + runner3 + " Followed by " + runner1 + "," + runner2);
- 05 Mar. 2013 09:22pm #3
- Age
- 97
- Join Date
- Nov. 2009
- Location
- In the computer
- Posts
- 11,186
- Reputation
- 1029
- LCash
- 0.93
- Awards
- 05 Mar. 2013 09:27pm #4
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 05 Mar. 2013 09:42pm #5
Doesn't look like it would work.
A simpler and more efficient solution would be to put the input in an array then use the sort method.
Or math.min/math.mid/math.max
To get it trk the way you seem to intend is mapping out all the possible combinations of the winners. That would be approximately 9 different if/elseif blocks. 3 for each set of numbers.
Also OP would need something to determine tie games. Which should be done with an else statement as an ultimatum.
It seems simple enough, but I don't know too much of Java's syntax to shoehorn a solution.
- 05 Mar. 2013 09:48pm #6
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 05 Mar. 2013 09:54pm #7
- 05 Mar. 2013 09:55pm #8
Doesn't even compile for me.
- 05 Mar. 2013 09:59pm #9
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 05 Mar. 2013 10:06pm #10
Yeah, this computer just sucks. It looks like it would work to me. Just can't get the code to compile.
- 05 Mar. 2013 10:08pm #11
- 05 Mar. 2013 10:30pm #12
'Murica, Also, why not just store results in multidimensional array then have a static size and set the size to 3. This way everything is dynamic and you can change the size to anything and have any amount of players and display only the top 3 best times
I hate when teachers aim so low for students. Ugh.
- 05 Mar. 2013 10:50pm #13
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 05 Mar. 2013 10:58pm #14
This. Initially I was looking at OP's problem from a lot of different angles. He could have done it 3+ different ways. 2 of which are simpler, more efficient ways than mapping out the 6 different combinations via if/elseif statements.
I've learned that typically in programming courses you utilize mechanisms taught throughout the course/unit rather than doing something esoteric or out of the ordinary. Sometimes you can even be failed for not doing it according to the given instructions.
- 06 Mar. 2013 12:35am #15
- Age
- 97
- Join Date
- Nov. 2009
- Location
- In the computer
- Posts
- 11,186
- Reputation
- 1029
- LCash
- 1.11
- Awards
Thanks to everyone that posted but stapled is right i havent gotten to arrays or whatever im up to the way stapled did it.
Im still not home to try it but im sure it works.
- 06 Mar. 2013 12:38am #16
- 06 Mar. 2013 12:46am #17
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 06 Mar. 2013 12:46am #18
- Age
- 97
- Join Date
- Nov. 2009
- Location
- In the computer
- Posts
- 11,186
- Reputation
- 1029
- LCash
- 0.83
- Awards
- 06 Mar. 2013 02:29am #19
Thats fucking stupid. There are an infinite number of ways you can code the same thing. Coding to his style is ridiculous. You should be put into teams and have to make one piece of full software the entire tri or semester. Then report to the teacher every now and then just as you would to a customer in real life. You'd learn so much more I think.
- 06 Mar. 2013 02:34am #20
Yo Matt, we're all entitled to an opinion. That's yours. But coding to a regiment or style could have its perks as well. Most languages will make you follow a somewhat strict rule set anyway.
Coding the way he instructs exercises discipline. Also, using structures/mechanisms practiced determines whether you've adequately learned the concepts or not.
Sure there are also downsides, but it's not always my way or the highway. The real world doesn't work like that.
- 06 Mar. 2013 02:35am #21
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
I don't think it's stupid but it's definitely not the best way, but how else would the teacher know the students are understanding the material. I recently finished an assignment for my web programming class and the only thing we had to follow code wise to use a flat file system instead of a database.
- 06 Mar. 2013 02:41am #22