| View previous topic :: View next topic |
| Author |
Message |
Doomwinner Advanced Cheater
Reputation: -1
Joined: 07 Oct 2009 Posts: 79 Location: USA
|
Posted: Tue Feb 19, 2013 7:40 pm Post subject: A bit of help with Java Programming. |
|
|
I need to set the program to list user-entered names and times in the order they finish.
I simply don't know how to set an order.
Here's what I have so far:
| Code: | /*
Write a program that asks for three runners and their time (in minutes).
Set the program to order them in the order they finish.
*/
import javax.swing.JOptionPane;
public class Racers
{
public static void main (String[] args)
{
String Racer1N;
String Racer2N;
String Racer3N;
Racer1N = JOptionPane.showInputDialog("Enter the first racer's name");
Racer2N = JOptionPane.showInputDialog("Enter the second racer's name");
Racer3N = JOptionPane.showInputDialog("Enter the third racer's name");
String Racer1;
Double Racer1D;
Racer1 = JOptionPane.showInputDialog("Enter the first racer's time (In minutes only).");
Racer1D = Double.parseDouble(Racer1);
String Racer2;
Double Racer2D;
Racer2 = JOptionPane.showInputDialog("Enter the second racer's time (In minutes only).");
Racer2D = Double.parseDouble(Racer2);
String Racer3;
Double Racer3D;
Racer3 = JOptionPane.showInputDialog("Enter the third racer's time (In minutes only).");
Racer3D = Double.parseDouble(Racer3);
JOptionPane.showMessageDialog(null,);
}
} |
_________________
Do I frustrate you? How about now? |
|
| Back to top |
|
 |
Screitor Cheater
Reputation: 1
Joined: 26 Nov 2012 Posts: 33 Location: Venezuela
|
Posted: Tue Feb 19, 2013 11:57 pm Post subject: |
|
|
There is a statement called "if"...
And... http://goo.gl/oV2JM.
_________________
Everybody lies. |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Wed Feb 20, 2013 1:40 am Post subject: |
|
|
Store the data in some sort of container and sort the containers objects based on the racers times.
_________________
- Retired. |
|
| Back to top |
|
 |
deviluc Cheater
Reputation: 1
Joined: 02 Jun 2010 Posts: 28
|
Posted: Wed Feb 20, 2013 8:24 am Post subject: |
|
|
I suppose the racer with the shortest time is the fastest one.
If you're just dealing with 3 racers, you can do a simple if-comparison:
| Code: |
String 1 = new String;
String 2 = new String;
String 3 = new String;
if (Racer1D < Racer2D) {
if (Racer1D < Racer3D) {
1 = Racer1N;
if (Racer2D < Racer3D) {
2 = Racer2N;
3 = Racer3N;
} else {
2 = Racer3N;
3 = Racer2N;
}
} else {
2 = Racer1N;
3 = Racer2N;
1 = Racer3N;
}
} else if (Racer1D > Racer2D) {
if (Racer1D > Racer3D) {
3 = Racer1N;
if (Racer2D > Racer3D) {
2 = Racer2N;
3 = Racer3N;
} else {
2 = Racer3N;
3 = Racer2N;
}
} else {
2 = Racer1N;
1 = Racer2N;
3 = Racer3N;
} |
With this code you'll store the fastest racer in String 1 and the slowest in String 3.
But if you want to sort more than just 3 times, you'll have to use quickSort (which is relative simple aswell)
|
|
| Back to top |
|
 |
Doomwinner Advanced Cheater
Reputation: -1
Joined: 07 Oct 2009 Posts: 79 Location: USA
|
Posted: Thu Feb 21, 2013 1:24 pm Post subject: |
|
|
I entered your code, but it complains of "reaching the end while parsing."
Can't figure out how to fix it.
Edit: Well, I am stuck, and I have to turn it in today. Ah well.
| Code: | /*
Write a program that asks for three runners and their time (in minutes).
Set the program to order them in the order they finish.
*/
import javax.swing.JOptionPane;
public class Racers
{
public static void main (String[] args)
{
String Racer1N;
String Racer2N;
String Racer3N;
Racer1N = JOptionPane.showInputDialog("Enter the first racer's name");
Racer2N = JOptionPane.showInputDialog("Enter the second racer's name");
Racer3N = JOptionPane.showInputDialog("Enter the third racer's name");
String Racer1;
Double Racer1D;
Racer1 = JOptionPane.showInputDialog("Enter the first racer's time (In minutes only).");
Racer1D = Double.parseDouble(Racer1);
String Racer2;
Double Racer2D;
Racer2 = JOptionPane.showInputDialog("Enter the second racer's time (In minutes only).");
Racer2D = Double.parseDouble(Racer2);
String Racer3;
Double Racer3D;
Racer3 = JOptionPane.showInputDialog("Enter the third racer's time (In minutes only).");
Racer3D = Double.parseDouble(Racer3);
String First;
String Second;
String Third;
if (Racer1D < Racer2D) {
if (Racer1D < Racer3D) {
First = Racer1N;
if (Racer2D < Racer3D) {
Second = Racer2N;
Third = Racer3N;
} else {
Second = Racer3N;
Third = Racer2N;
}
} else {
Second = Racer1N;
Third = Racer2N;
First = Racer3N;
}
} else if (Racer1D > Racer2D) {
if (Racer1D > Racer3D) {
Third = Racer1N;
if (Racer2D > Racer3D) {
Second = Racer2N;
Third = Racer3N;
} else {
Second = Racer3N;
Third = Racer2N;
}
} else {
Second = Racer1N;
First = Racer2N;
Third = Racer3N;
}
JOptionPane.showMessageDialog(null,First + Second + Third);
}
} |
_________________
Do I frustrate you? How about now? |
|
| Back to top |
|
 |
Chris12 Expert Cheater
Reputation: 1
Joined: 27 Apr 2012 Posts: 103
|
Posted: Thu Feb 21, 2013 11:49 pm Post subject: |
|
|
"reaching the end while parsing"
check if all '{' have a matching '}'
From what i can see you have 12 {
but only 11 }
|
|
| Back to top |
|
 |
deviluc Cheater
Reputation: 1
Joined: 02 Jun 2010 Posts: 28
|
Posted: Fri Feb 22, 2013 7:53 am Post subject: |
|
|
You forgot the last }, just put it at the end of the code and you took the inizialisation of the Strings out (that I put in), so here's your working code:
| Code: | /*
Write a program that asks for three runners and their time (in minutes).
Set the program to order them in the order they finish.
*/
import javax.swing.JOptionPane;
public class Racers
{
public static void main (String[] args)
{
String Racer1N;
String Racer2N;
String Racer3N;
Racer1N = JOptionPane.showInputDialog("Enter the first racer's name");
Racer2N = JOptionPane.showInputDialog("Enter the second racer's name");
Racer3N = JOptionPane.showInputDialog("Enter the third racer's name");
String Racer1;
Double Racer1D;
Racer1 = JOptionPane.showInputDialog("Enter the first racer's time (In minutes only).");
Racer1D = Double.parseDouble(Racer1);
String Racer2;
Double Racer2D;
Racer2 = JOptionPane.showInputDialog("Enter the second racer's time (In minutes only).");
Racer2D = Double.parseDouble(Racer2);
String Racer3;
Double Racer3D;
Racer3 = JOptionPane.showInputDialog("Enter the third racer's time (In minutes only).");
Racer3D = Double.parseDouble(Racer3);
String First = new String();
String Second = new String();
String Third = new String();
if (Racer1D < Racer2D) {
if (Racer1D < Racer3D) {
First = Racer1N;
if (Racer2D < Racer3D) {
Second = Racer2N;
Third = Racer3N;
} else {
Second = Racer3N;
Third = Racer2N;
}
} else {
Second = Racer1N;
Third = Racer2N;
First = Racer3N;
}
} else if (Racer1D > Racer2D) {
if (Racer1D > Racer3D) {
Third = Racer1N;
if (Racer2D > Racer3D) {
Second = Racer2N;
Third = Racer3N;
} else {
Second = Racer3N;
Third = Racer2N;
}
} else {
Second = Racer1N;
First = Racer2N;
Third = Racer3N;
}
}
JOptionPane.showMessageDialog(null,First + ", " + Second + ", " + Third);
}
} |
|
|
| Back to top |
|
 |
|