当输入为正数时,可以正确运行!但是当输入为字母时,系统会运行出错,
如何不让系统报错,而实现程序继续运行,产生一个"输入有误的语句"!import javax.swing.JOptionPane;
public class Sort
{
public static void main(String[] args){
int a,b,c,d;
int temp;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c = Integer.parseInt(args[2]);
d = Integer.parseInt(args[3]);
if(a>b){temp=a;a=b;b=temp;}
if(a>c){temp=a;a=c;c=temp;}
if(a>d){temp=a;a=d;d=temp;}
if(b>c){temp=b;b=c;c=temp;}
if(b>d){temp=b;b=d;b=temp;}
if(c>d){temp=c;c=d;c=temp;}
JOptionPane.showMessageDialog(null,a+" "+b+" "+c+" "+d,"由小到大:",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
如何不让系统报错,而实现程序继续运行,产生一个"输入有误的语句"!import javax.swing.JOptionPane;
public class Sort
{
public static void main(String[] args){
int a,b,c,d;
int temp;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c = Integer.parseInt(args[2]);
d = Integer.parseInt(args[3]);
if(a>b){temp=a;a=b;b=temp;}
if(a>c){temp=a;a=c;c=temp;}
if(a>d){temp=a;a=d;d=temp;}
if(b>c){temp=b;b=c;c=temp;}
if(b>d){temp=b;b=d;b=temp;}
if(c>d){temp=c;c=d;c=temp;}
JOptionPane.showMessageDialog(null,a+" "+b+" "+c+" "+d,"由小到大:",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
public class Sort{
public static void main(String[] args){
try{
int a,b,c,d;
int temp;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c = Integer.parseInt(args[2]);
d = Integer.parseInt(args[3]);
if(a>b){temp=a;a=b;b=temp;}
if(a>c){temp=a;a=c;c=temp;}
if(a>d){temp=a;a=d;d=temp;}
if(b>c){temp=b;b=c;c=temp;}
if(b>d){temp=b;b=d;b=temp;}
if(c>d){temp=c;c=d;c=temp;}
JOptionPane.showMessageDialog(null,a+" "+b+" "+c+" "+d,"由小到大:",JOptionPane.INFORMATION_MESSAGE);
}catch(Exception e) {System.out.println ("输入有误的语句");}
}
}
加一个异常处理,不会就找找教程
建议输入的时候使用输入流会更好处理InputStream
Swing类里也有输入的,自己学学吧