我为我的班制作了这个程序,但是当它运行时它只是随机排序名称而不是按数字顺序(升序)对它们进行排序。我不知道我是否有任何错误,但我猜这个问题出现在下面的“ifs”中:
import javax.swing.JOptionPane;
public class RaceOrderApp {
public static void main(String[] args) {
String racer1, racertime1str, racer2, racertime2str, racer3, racertime3str;
int racerTime1, racerTime2, racerTime3;
racer1 = JOptionPane.showInputDialog(null, "Enter the name of racer #1:" );
if (racer1 == null || racer1.length() == 0) {
JOptionPane.showMessageDialog(null, "You did not enter a name. The application will end.");
System.exit(1);}
racertime1str = JOptionPane.showInputDialog(null, "Enter the time of " + racer1 + ":" );
racerTime1 = Integer.parseInt(racertime1str);
if (racerTime1 >= 15 || racerTime1 <= 100) {
JOptionPane.showMessageDialog(null, "You did not enter a valid time. The application will end.");
System.exit(1);}
racer2 = JOptionPane.showInputDialog(null, "Enter the name of racer #2:" );
if (racer2 == null || racer2.length() == 0) {
JOptionPane.showMessageDialog(null, "You did not enter a name. The application will end.");
System.exit(1);}
racertime2str = JOptionPane.showInputDialog(null, "Enter the time of " + racer2 + ":" );
racerTime2 = Integer.parseInt(racertime2str);
if (racerTime2 >= 15 || racerTime2 <= 100) {
JOptionPane.showMessageDialog(null, "You did not enter a valid time. The application will end.");
System.exit(1);}
racer3 = JOptionPane.showInputDialog(null, "Enter the name of racer #3:" );
if (racer3 == null || racer3.length() == 0) {
JOptionPane.showMessageDialog(null, "You did not enter a name. The application will end.");
System.exit(1);}
racertime3str = JOptionPane.showInputDialog(null, "Enter the time of " + racer3 + ":" );
racerTime3 = Integer.parseInt(racertime3str);
if (racerTime3 >= 15 || racerTime3 <= 100) {
JOptionPane.showMessageDialog(null, "You did not enter a valid time. The application will end.");
System.exit(1);}
String firstRacer = racer1 + " " + racerTime1;
String secondRacer = racer2 + " " + racerTime2;
String thirdRacer = racer3 + " " + racerTime3;
if (racerTime1 > racerTime2) {
String temp = firstRacer;
firstRacer = secondRacer;
secondRacer = temp;
}
if (racerTime2 > racerTime3) {
String temp = secondRacer;
secondRacer = thirdRacer;
thirdRacer = temp;
}
if (racerTime1 > racerTime2 ) {
String temp = firstRacer;
firstRacer = secondRacer;
secondRacer = temp;
}
String mensaje="The order of the racers is:\n";
mensaje += "1st. " + firstRacer + "\n";
mensaje += "2nd. " + secondRacer + "\n";
mensaje += "3rd. " + thirdRacer + "\n";
JOptionPane.showMessageDialog(null, mensaje);
}
}
提前感谢任何可以提供帮助的人!
答案 0 :(得分:0)
你遇到问题if statements
(racerTime2 >= 15 || racerTime2 <= 100)
标志有问题
它表示如果大于15或小于100,则必须大于100或小于15:
if (racerTime2 <= 15 || racerTime2 >= 100)
由于您未使用if statements
或;
else
应以else if
结尾