JAVA基础练习2

1.求数字阶乘

接收键盘录入的数字,计算该数字的阶乘结果,已知:负数不可以有阶乘,0的阶乘结果是1
//自定义一个异常类
public class NoNumberException extends Exception{
    public NoNumberException(){

    }
    public NoNumberException(String message){
        super(message);
    }
}
//测试类
public class t10 {
    public static void main(String[] args) throws NoNumberException {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个数字");
        int num = sc.nextInt();
        getNum(num);

    }
    public static void getNum(int num) throws NoNumberException {
        if (num < 0){
            throw  new NoNumberException("小子,不能是负数");
        }
        if (num == 0){
            System.out.println("0的阶乘是1");
        }
        long r = num;
        for (int i = num-1; i >= 1; i--) {
            r *= i;
        }
        System.out.println(num + "的阶乘是" + r);
    }
}

2.多次生成随机数,并打印第一次出现大于0.999 时的次数与生成的随机数

public class t11 {
    public static void main(String[] args) {
        Random rd = new Random();
        int count = 0;
        while(true){
            double nums =  rd.nextDouble();
            count ++ ;//计算次数
            if (nums > 0.999){
                System.out.println("这是第" + count +"次出现大于0.999的数");
                break;
            }
        }
    }
}

3.打印100以内除了尾数为3,5,7的所有数

public class t12 {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            int a = i % 10;
            if (a ==3 || a == 5 || a == 7){
                continue;
            }
            System.out.println(i);

        }
    }
}

4.键盘录入一个整数判断是否为质数

public class t13 {
    public static void main(String[] args) {
        System.out.println("输入一个整数");
        int num =new Scanner(System.in).nextInt();

    }
    public static  void show( int num){
        if (num == 1){
            System.out.println("小子这个数"+ num + "不是质数");
            return;
        }

    }
}

5.生成一个顺序数组,将这个数组的元素打乱顺序后输出

public class t14 {
    public static void main(String[] args) {
        int [] array = new int[]{};
        array = setArray();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println("=================");
        shuffle(array);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
    public static int[] setArray(){
        int [] a = new int[6];
        for (int i = 0; i < a.length; i++) {
            a[i] = i+1;
        }
        return a;
    }
    public static void  shuffle(int[] array){
        /*
          i循环遍历数组
         随机定位下标j与i交换
         */
        for (int i = 0; i < array.length; i++) {
            int c = new Random().nextInt(array.length);
            System.out.println(c);
            int t = array[i];
            array[i] = array[c];
            array[c] = t;

        }
    }
}

6.接收用户输入的任意自然数,累计所有位数数字之和

public class t15 {
    public static void main(String[] args) {
        System.out.println("请输入一个自然数:");
        int n = new Scanner(System.in).nextInt();
        //定义求和变量
        int sum = 0;
        //while循环获取每一位的数子
        while(n != 0){
            //求和累加
            sum = sum + (n%10);
            //去掉一位数字,将下一位变为个位
            n = n / 10;
        }
        System.out.println("输入的自然数,每位数之和为:" + sum);
    }
}

7.求任意数组中所有元素的最大值

public class t16 {
    public static void main(String[] args) {
        int [] arr = {100,20,30,66,999,10,3};
        //定义一个最大值
        int max = arr[arr.length-1];
        //遍历数组
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max){
                //当前数组内所有数组进行比较,如果有大于max的则赋值给max
                max = arr[i];
            }
        }
        System.out.println("最大值是:" + max);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值