/**
* 5-1 求阶乘factorial (10分)
编程从键盘输入一个整数,计算出阶乘并输出。
输入格式:
输入 39
输出格式:
输出:20397882081197443358640281739902897356800000000
输入样例:
58
输出样例:
2350561331282878571829474910515074683828862318181142924420699914240000000000000
*/
package text1125;
import java.math.BigInteger;
import java.util.Scanner;
public class java13test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int n=input.nextInt();
BigInteger m = BigInteger.ONE;
for(int i=1;i<=n;i++){
m=m.multiply(BigInteger.valueOf(i));
}
System.out.println(m);
}
}
5-2 数字格式异常 (10分)
(NumberFormatException数字格式异常)编写一个程序,提示用户读取两个整数,然后显示他们的和。程序应该在输入不正确时提示用户再次输入数字。
输入格式:
i 9 (第1次输入) l 8 (第2次输入) 5 6 (第3次输入)
输出格式:
Incorrect input and re-enter two integers: (第1次输出提示) Incorrect input and re-enter two integers: (第2次输出提示) Sum is 11 (输出结果)
输入样例:
i 9
l 8
5 6
输出样例:
Incorrect input and re-enter two integers:
Incorrect input and re-enter two integers:
Sum is 11
package text1125;
import java.util.*;
public class text2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
boolean continueInput=true;
do{
try{
//System.out.println("Incorrect input and re-enter two integers:");
int a=input.nextInt();
int b=input.nextInt();
System.out.println("Sum is "+(a+b));
continueInput=false;
//System.out.println("这句话执行了!");
}
catch(InputMismatchException e){
System.out.println("Incorrect input and re-enter two integers:");
input.nextLine();//input.nextLine()的作用就是,将 输入流标记位 移动到下一个输入位、置.Discard input 抛弃输入
}
}while(continueInput);
}
}