public class test {
public static void main(String[] args) {
System.out.println("请输入计算x!的x值");
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int x = m;
int sum = 1;
if (m>0){
while (m > 0) {
sum = m *sum;
m = --m;
}
System.out.println(x+"!="+sum);
}
else System.out.println("无法计算");
scanner.close();
}
}
缺点:输入非int类型数据报错
scanner使用步骤:
标签:java,scanner,int,sum,System,println,阶乘,out From: https://www.cnblogs.com/exact/p/16790770.html1.new一个Scanner
2.scanner.next()
3.关闭scanner