A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

Program help

1.4k views · started by Taz ·
#1
Program help
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.

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);









}
}
#2
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);
#3
Stapled wrote:

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);


Thank you so much when i get home ima try this.
Does the rest of the program look alright or am i going have to add or change something?
#4
As long as the time is entered in seconds that is the only thing that needs to be changed.
#5
Stapled wrote:

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);


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 to work 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.
#6
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 to work 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.


1. It does work
2. I simply fixed the errors caused by his original code.
#7
Stapled wrote:
1. It does work
2. I simply fixed the errors caused by his original code.


Did you notice that if you put your avatar pic with TU, it looks like you're looking at each other? Cute.
#8
Doesn't even compile for me.
#9
Doesn't even compile for me.


You have to replace the outcome part of his code with what I posted. If if doesn't work you goofed, I tested it.

#10
Yeah, this computer just sucks. It looks like it would work to me. Just can't get the code to compile.
#11
Stapled wrote:
You have to replace the outcome part of his code with what I posted. If if doesn't work you goofed, I tested it.



See? Tell me I wasn't right.
#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.
#13
MattSmith wrote:
'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.


Maybe they haven't started those topics yet?
#14
Stapled wrote:
Maybe they haven't started those topics yet?


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.
#15
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.
#16
Just because the teacher hasn't taught it doesn't mean you can't do it. I haven't taken classes to learn anything I know, simply use google and be enthusiastic about what you're making.
#17
MattSmith wrote:
Just because the teacher hasn't taught it doesn't mean you can't do it. I haven't taken classes to learn anything I know, simply use google and be enthusiastic about what you're making.


In my early web programming class the professor would give us a problem and we would have to code the solution what he had just taught us. If you used something different he would make you redo it.
#18
MattSmith wrote:
Just because the teacher hasn't taught it doesn't mean you can't do it. I haven't taken classes to learn anything I know, simply use google and be enthusiastic about what you're making.


For right now i just want to stay with the teacher until i get the hang of things then i will learn more from there.
#19
Stapled wrote:
In my early web programming class the professor would give us a problem and we would have to code the solution what he had just taught us. If you used something different he would make you redo it.


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.
#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.
#21
MattSmith wrote:
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.


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.
#22
Stapled wrote:
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.

Ah, that's more reasonable! My programming classes at my highschool you just retype whats on the paper and hit compile. Like the fuck, you learn NOTHING BY FUCKING RETYPING THE PIECE OF PAPER. So I just dont waste my time.