我这个判断是拿try catch 写的,谁能告诉我一下拿正则表达式怎么写呀??求给写一下 菜鸟一枚
/**
* 求1+2+3+...n 的阶乘
*/
package JC;
import java.util.Scanner;
public class CopyOfJieCheng {
public static void main(String[] args) {
int sum1 = 1;
int sum2 = 0;
System.out.print("请输入数字:");
Scanner Scan = new Scanner(System.in);//获取输入的数据
// System.out.println("你输入的数字是" + Scan);
int n = 0;
boolean flag = true;
do {
try {
String scan = Scan.nextLine();
n = Integer.parseInt(scan);
//Integer.parseInt(String)遇到一些不能被转换为整型的字符时,会抛出异常
flag = false;
} catch (Exception e) {
// TODO: handle exception
//e.printStackTrace();
System.out.println("输入错误,请重新输入");
System.out.print("请输入数字:");
}
} while (flag);
System.out.print("你输入的数字是" + n + '\n');
if (n < 0) {
System.out.print("输入错误,请重新输入");
}
if (n == 0) {
System.out.print("0的阶乘是1" + '\n');
}
if (n > 0) {
for (int i = 1; i <= n; i++) {
sum1 = sum1 * i;
sum2 = sum2 + sum1;
System.out.print(i + "的阶乘是" + sum1 + '\n');
// System.out.println("前" + i + "个数的阶乘和是" + sum2);
}
System.out.print("前" + n + "个数的阶乘和是" + sum2 + '\n');
}
}
}
------解决方案--------------------1.正则:^[+,-]*[0-9]+$。2.else{
//应该写个重新取数据的操作
}
------解决方案--------------------