Results 1 to 8 of 8
Thread: Need help in a program
- 11-04-2009, 10:27 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
Need help in a program
this programs supose to get 3 names and 3 times and then put them in order from the minior number to the mayor number plss help me solve this problem
import javax.swing.JOptionPane;
public class racersTime {
/**
* @param args
*/
public static void main(String[] args) {
String inputString;
String name1, name2, name3,temp2;
double time1, time2, time3;
double temp1;
inputString = JOptionPane.showInputDialog("Enter the name of the contestant:");
name1 = inputString;
inputString = JOptionPane.showInputDialog("Enter the time of the contestan:");
time1= Double.parseDouble(inputString);
inputString = JOptionPane.showInputDialog("Enter the name of the contestant:");
name2 = inputString;
inputString = JOptionPane.showInputDialog("Enter the time of the contestan:");
time2= Double.parseDouble(inputString);
inputString = JOptionPane.showInputDialog("Enter the name of the contestant:");
name3 = inputString;
inputString = JOptionPane.showInputDialog("Enter the time of the contestan:");
time3= Double.parseDouble(inputString);
if (time1 > time2)
{temp1=time1;
time1=time2;
time2=temp1;}
{temp2=name1;
name1=name2;
name2=temp2;}
if(time2 > time3)
{temp1=time2;
time2=time3;
time3=temp1;}
{temp2=name2;
name2=name3;
name3=temp2;}
if(time1 > time3 )
{temp1=time1;
time1=time3;
time3=temp1;}
{temp2=name1;
name1=name3;
name3=temp2;}
JOptionPane.showMessageDialog(null," The contestant from the first to the last: "+
name1+","+time1 + " " + name2 + ","+time2+ " " + name3+","+time3);
System.exit(0);
}
}
thank for your needed help :):)
-
Two suggestions:
1) Please edit your post so that it uses code tags (see my signature with its link below). Doing this will make your code much more readable, and will increase your chances of getting help.
2) Also, if you provide more details as to exactly what your current problem is, you'll also increase your chances here.
Much luck!
- 11-04-2009, 11:13 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
Tnks im new here :)
-
- 11-04-2009, 11:20 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
Wen i run the program this supose to put the three names and the three times in order
of minior to mayor and went i put this numbers 12, 4, 8 the program don't give me it corectly
Ex.of wath i want to the program i enter:
Jonathan 12.54 -seconds
Joel 10.00 - seconds
Erick - 8.0 - seconds
the program supose to give me this output: erick,8.00 joel,10.00 jonathan, 12.54
-
That seems correct to me, since for an auto racer, a shorter time is better.
Edit: oh, I see. It is not outputting this result. Well you might want to post what it is in fact outputting, but even more important, I recommend that you go through on paper just what your program is doing:
Let's get rid of the names and just use letters. Say you input
A 12
B 10
C 8
The first if block will check the first two racers and swap them if 1 is greater than 2. This will result in:
B 10
A 12
C 8
The next if block will swap the 2nd and 3rd racers if 2 is greater than 3 (which again it is), resulting in:
B 10
C 8
A 12
The last if block will swap the 3rd and first racers if first is greater than the third. Since this isn't so, no swap will be done, and your result will be as noted above.
Given this reasoning, you should be able to figure out a solution, right?
Also, this is dangerous:
As you'll swap names in the second block whether or not the if is true. Better is:Java Code:if (time1 > time2) { temp1 = time1; time1 = time2; time2 = temp1; } { temp2 = name1; name1 = name2; name2 = temp2; }
Java Code:if (time1 > time2) { temp1 = time1; time1 = time2; time2 = temp1; temp2 = name1; name1 = name2; name2 = temp2; }Last edited by Fubarable; 11-04-2009 at 11:44 PM.
- 11-05-2009, 12:20 AM #7
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
Thanks realy now i solve my error you safe me in this homework :)
:):):)
-
You're welcome, and again, welcome to the forum!
Similar Threads
-
execute java program within java program
By popey in forum New To JavaReplies: 2Last Post: 10-22-2009, 05:32 PM -
Execute A program from a Program!
By Moncleared in forum Advanced JavaReplies: 2Last Post: 02-22-2009, 04:17 PM -
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks