基础编程练习题

这篇博客汇总了从基础编程题01到基础编程题09的Java编程练习,覆盖了多个基础编程概念和技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)基础编程题01


 * 编程实现:根据以下函数关系,对输入的X值计算输出对应的y值。
 *
 * x的值	对应y的值
 * x<0	       0
 * 0<=x<10	   x
 * 10<=x<20	0.5*x+18
 * x>=20	  100
 
public class Test01 {
   
    public static void main(String[] args) {
   
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入X值:");
        double x = scanner.nextDouble();
        double y = 0 ;
        //判断
        if (x < 0){
   
            y = 0;
        }else if ( 0 <= x && x <10 ){
   
            y = x;
        }else if (10 <= x && x < 20){
   
            y = 0.5 * x +18;
        }else if ( x >= 20){
   
            y = 100;
        }
        System.out.println("y对应的值为:" + y);

        scanner.close();
    }
}

 * 打印九九乘法表

public class Test02 {
   
    public static void main(String[] args) {
   
        for (int i = 1; i <= 9 ; i++) {
   
            for (int j = 1; j <= i ; j++) {
   
                System.out.print(j + "*" + i + "=" + j * i + " ");
            }
            System.out.println("");
        }
    }
}

 * 编写程序计算1+2+3++n!,并输出计算结果

public class Test03 {
   
    public static void main(String[] args) {
   
     Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个整数:");
        if (scanner.hasNextInt()){
   //判断是否是整数
        int x = scanner.nextInt();
            System.out.println("您输入的整数为:" + x);
        int sum = 0;
        for (int i = 1; i <= x ; i++) {
   
        int n =1;
            for (int j = 1; j <= i ; j++) {
   
                n *= j;
            }
            sum += n;
        }
        System.out.println(x + "!" + "=" + sum);
        }else {
   
            System.out.println("请输入有效值");
        }
        scanner.close();
    }
}

 * 编写三个方法,分别得出一个数组的最大值,最小值,平均值。

public class Test04 {
   
    public static void main(String[] args) {
   
        int[] array = {
   7, 2, 8, 3, 10};
        int max = numMax(array);
        int min = numMin(array);
        double avg = numAvg(array);
        System.out.println("最大值:" + max + " " + "最小值:" + min + " "+ "平均值:" + avg);
    }
   public static int numMax(int[] array){
   
        int max = array[1];
       for (int i = 0; i < array.length ; i++) {
   
           if (max < array[i]){
   
               max = array[i];
           }
       }
       return max;
   }
   public  static int numMin(int[] array){
   
        int min = array[1];
       for (int i = 0; i < array.length ; i++) {
   
           if (min > array[i]){
   
               min = array[i];
           }
       }
       return min;
   }
   public static double numAvg(int[] array){
   
        double avg;
        double sum =0;
       for (int i = 0; i < array.length ; i++) {
   
           sum += array[i];
       }
       avg = sum / array.length;
       return avg;
   }
}

 * 接收用户输入的5门功课并且,计算平均分。
 *
 * 给用户评级60-80良,81-90好,91-100优秀。
 
public class Test05 {
   
    public static void main(String[] args) {
   
        System.out.println("请输入5门功课");
        Scanner sc = new Scanner(System.in);
        double arr[] = new double[5];
        double avg;
        double sum = 0;

        if (sc.hasNextDouble()){
   //判断是否为数值
        //输入成绩
            for (int i = 0; i < 5; i++) {
   
                System.out.print("第" + (i + 1) + "门功课:");
                arr[i] = sc.nextDouble();
                sum += arr[i];
            }

            avg = sum / arr.length;

            String rank = (91 <= avg ? "优秀" : (81 <= avg ? "好" : (60 <= avg ? "良" : "")));
            System.out.println("五门科目的平均分是:" + avg + "\t评级为:" + rank);
            sc.close();
        }else {
   
            System.out.println("请输入0-100之内的数值");
        }
    }

}

(二)基础编程题02

	
 * 从键盘输入一行字符串(以换行符结束),要求分别统计里面英文字符的
 * 总个数和数字的总个数,并分别输出.

public class Test01 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        char[] arr = sc.nextLine().toCharArray();
        int sum, word = 0;
        for (int i = 0; i < arr.length ; i++) {
   
            int x = arr[i];
            if (x >= 'a' & x >= 'z'| x >= 'A' & x>= 'Z' ){
   
                word++;
            }else if (x >= '0' | x >= '9'){
   
                sum++;
            }
        }
        System.out.println("英文个数为:" + word);
        System.out.println("数字个数为:" + sum);
        sc.close();
    }
 
 * 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,
 * 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,
 * 问每个月的兔子对数为多少?(输出第10个月的兔子对数)

    public class Test02 {
   
        public static void main(String[] args) {
   
            for (int i = 1; i <= 10; i++) {
   
                int num = getNum(i);
                System.out.println("第" + i + "个月后,兔子对数为:" + num);
            }
        }
        public static int getNum(int i){
   
            if (i == 1 || i == 2){
   
                return 1;
            }else {
   
                return getNum(i - 1) + getNum(i - 2);
            }
        }
    }

 * 编写万用表程序
 * 1 电压挡 2 电流挡 3 电阻档 4 其他档位 5 退出

public class Test03 {
   
    public static void main(String[] args) {
   

        System.out.println("欢迎使用电能表");
        System.out.println("1 电压挡 2 电流挡 3 电阻档 4 其他档位 5 退出");
        System.out.println("请输入你想使用的挡位:");
        Scanner sc = new Scanner(System.in);
        String  input = sc.next();
        //判断选择
        while (!input.equals("1") && !input.equals("2") && !input.equals("3") &&
                !input.equals("3") && !input.equals("4") && !input.equals("5")){
   
            System.out.println("请选择有效选项");
              input = sc.next();
        }
        //挡位选择
        switch (input){
   
            case "1":
                System.out.println("电压挡");
                break;
            case "2":
                System.out.println("电流挡");
                break;
            case "3":
                System.out.println("电阻档");
                break;
            case "4":
                System.out.println("其他档位");
                break;
            case "5":
                System.out.println("退出");
                break;
        }
        sc.close();
    }
}
public class Test04 {
   

     * 判断101-200之间有多少个素数,并输出所有素数

        public static void main(String[] args) {
   
            int count = 0;
            for (int i = 101; i <= 200 ; i++) {
   
                boolean b = true;
                for (int j = 2; j < i ; j++) {
   
                    if (i % j == 0){
   
                        b = false;
                        break;
                    }else {
   
                        b = true;
                    }
                }
                if (b){
   
                    System.out.println
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值