首页 > 编程语言 >java阶乘的实现(scanner使用练习)

java阶乘的实现(scanner使用练习)

时间:2022-10-14 10:37:44浏览次数:48  
标签:java scanner int sum System println 阶乘 out

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使用步骤:

1.new一个Scanner
2.scanner.next()
3.关闭scanner

标签:java,scanner,int,sum,System,println,阶乘,out
From: https://www.cnblogs.com/exact/p/16790770.html

相关文章