Java 排序应用程序基于用户输入(名称和运行时间),并按升序数字顺序进行排序
我为我的类编写了这个程序,但当它运行时,它只是对名称进行随机排序,而不是按数字顺序(升序)排序。我不知道我是否有任何错误,但我猜问题出在下面的“如果”中:Java 排序应用程序基于用户输入(名称和运行时间),并按升序数字顺序进行排序,java,Java,我为我的类编写了这个程序,但当它运行时,它只是对名称进行随机排序,而不是按数字顺序(升序)排序。我不知道我是否有任何错误,但我猜问题出在下面的“如果”中: import javax.swing.JOptionPane; 公共类RaceOrderApp{ 公共静态void main(字符串[]args){ 线状消旋体1、消旋体1、消旋体2、消旋体2、消旋体3、消旋体3; 内消旋体1,消旋体2,消旋体3; racer1=JOptionPane.showInputDialog(null,“输入race
import javax.swing.JOptionPane;
公共类RaceOrderApp{
公共静态void main(字符串[]args){
线状消旋体1、消旋体1、消旋体2、消旋体2、消旋体3、消旋体3;
内消旋体1,消旋体2,消旋体3;
racer1=JOptionPane.showInputDialog(null,“输入racer的名称#1:”);
if(racer1==null | | racer1.length()==0){
showMessageDialog(null,“您没有输入名称。应用程序将结束”);
系统退出(1);}
racertime1str=JOptionPane.showInputDialog(null,“输入“+racer1+”:”的时间);
racerTime1=Integer.parseInt(racertime1str);
如果(racerTime1>=15 | | racerTime1=15 | | racerTime2=15 | | racerTime3 racerTime2){
字符串温度=firstRacer;
第一赛车手=第二赛车手;
第二消旋器=温度;
}
如果(racerTime2>racerTime3){
字符串温度=第二消旋器;
第二赛车手=第三赛车手;
第三者=温度;
}
如果(racerTime1>racerTime2){
字符串温度=firstRacer;
第一赛车手=第二赛车手;
第二消旋器=温度;
}
String mensaje=“参赛者的顺序是:\n”;
门萨耶+=“第一名”+“第一名赛车手”+“\n”;
门萨耶+=“第二名”+“第二名”+“\n”;
门萨耶+=“第三名”+“第三名”+“\n”;
showMessageDialog(null,mensaje);
}
}
提前感谢任何能提供帮助的人 您的if语句有问题
(racerTime2>=15 | | racerTime2>=15 | |你是否使用调试器来逐步调试代码?是的,它在开始时的if语句是错误的,因此我修复了它们,并进行了一些变通,结果成功了!非常感谢!我对代码进行了一些变通,并使其生效。我还修复了if语句。
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);
}
}