public class BJA { public static void main(String[] args) {
try{
int sum;
int j,n; if (args.length != 2) {
System.out.println("参数错误请重试:");
} else {
sum = j = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]); if (j > n) {
System.out.print("命令行参数不规范!第1个数比第2个数大!");
} else {
if (j == n) {
System.out.print("命令行参数不规范!2个数相等!");
} else {
for (int i = j + 1; i <= n; i++) {
System.out.println(sum + "+" + i + "=" + (i + sum));
sum = sum + i;
}
}
}
}
}
catch(Exception e){
System.out.println("异常处理请从试,您输入的不是数字");
}
/* catch(Exception e){
System.out.println("异常处理,您输入的第一个参数不是数字");
}*/
}
}
请问大家我想让程序运行后如果我输入2个参数都不是数字提示我输入的参数不是数字,然后我输入1个参数的时候并且不是数字提示我输入的第一个参数不是数字我该如何写第2个异常?
try{
int sum;
int j,n; if (args.length != 2) {
System.out.println("参数错误请重试:");
} else {
sum = j = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]); if (j > n) {
System.out.print("命令行参数不规范!第1个数比第2个数大!");
} else {
if (j == n) {
System.out.print("命令行参数不规范!2个数相等!");
} else {
for (int i = j + 1; i <= n; i++) {
System.out.println(sum + "+" + i + "=" + (i + sum));
sum = sum + i;
}
}
}
}
}
catch(Exception e){
System.out.println("异常处理请从试,您输入的不是数字");
}
/* catch(Exception e){
System.out.println("异常处理,您输入的第一个参数不是数字");
}*/
}
}
请问大家我想让程序运行后如果我输入2个参数都不是数字提示我输入的参数不是数字,然后我输入1个参数的时候并且不是数字提示我输入的第一个参数不是数字我该如何写第2个异常?
try
{
sum = j = Integer.parseInt(args[0]);
}
catch(Exception e){
System.out.println("异常处理,您输入的第一个参数不是数字");
}
try
{
n = Integer.parseInt(args[1]);
}
catch(Exception e){
System.out.println("异常处理,您输入的第二个参数不是数字");
}
int sum;
int j,n; if (args.length != 2) {
System.out.println("参数错误请重试:");
}
else {
try{ sum = j = Integer.parseInt(args[0]);
}
catch(Exception e){
System.out.println("异常处理请从试");
}
try{ n = Integer.parseInt(args[1]);
}
catch(Exception e){
System.out.println("dfdfdf");
}
if (j > n) {
System.out.print("命令行参数不规范!第1个数比第2个数大!");
} else {
if (j == n) {
System.out.print("命令行参数不规范!2个数相等!");
} else {
for (int i = j + 1; i <= n; i++) {
System.out.println(sum + "+" + i + "=" + (i + sum));
sum = sum + i;
}
}
}
} }
}
你的意思是这样?
package test2;public class test2 { public static void main(String[] args) {
int sum=0; //注意这里,try中的程序可能不被执行,必须初始脂,不然会报错
int j=0, n=0; if (args.length != 2) {
System.out.println("参数错误请重试:");
System.exit(0);
} else {
try { sum = j = Integer.parseInt(args[0]);
} catch (Exception e) {
System.out.println("第一个数不规范");
System.exit(0);
}
try { n = Integer.parseInt(args[1]);
} catch (Exception e) {
System.out.println("第二个数不规范");
System.exit(0);
}
if (j > n) {
System.out.print("命令行参数不规范!第1个数比第2个数大!");
} else {
if (j == n) {
System.out.print("命令行参数不规范!2个数相等!");
} else { for (int i = j + 1; i <= n; i++) {
System.out.println(sum + "+" + i + "=" + (i + sum));
sum = sum + i;
} }
}
} }
}
try
{
j = Integer.parseInt(args[0]);
}
catch(Exception e){
b1 = true;
}
try
{
n = Integer.parseInt(args[1]);
}
catch(Exception e){
b2 = true;
}
if (b1 && b2)
{
System.out.println("输入的参数都不是数字");
}
else if (b1)
{
System.out.println("输入的第一个参数不是数字");
}
else if (b2)
{
System.out.println("输入的第二个参数不是数字");
}