Java SE基础练习题

本文是一系列基于Java的编程练习题目,涵盖基础算法、逻辑运算、数学问题等多个方面,适合初学者提升Java编程能力。
摘要由CSDN通过智能技术生成
  1. 不死兔子: 小明今年高考,考了700,父母,给他买了一对刚刚出生小兔子,四个月后成长为成年兔子,成年后,每过一个月,假设生出一对新的小兔子
    问:第n月,小明家共有多少对兔子。
import java.util.Scanner;

public class Demo01 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入月份:");
        int n = sc.nextInt();
        int num = rabbit(n);
        System.out.println(num);
    }

    public static int rabbit(int n){
   
        int num = 1;
        if (n <= 4){
   
            return 1;
        }
        return rabbit(n - 1) + rabbit(n - 4);
    }
}

课堂练习:
1.打印

*			1
**			2
***			3
****		4
*****		5
public class practice1 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入你要打印的行数:");
        int n = sc.nextInt();
        print(n);
    }

    public static void print(int n){
   
        for (int i = 1; i <= 5; i++){
   
            for (int j = 1; j <= i; j++){
   
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

2.九九乘法表

/*
  九九乘法表
  1×1=1
  1×2=2 2×2=4
  1×3=3 2×3=6 3×3=9
  1×4=4 2×4=8 3×4=12 4×4=16
  1×5=5 2×5=10 3×5=15 4×5=20 5×5=25
  1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
  1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
  1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
  1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
 */
public class practice2 {
   
    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 + "=" + (i*j) + " ");
            }
            System.out.println();
        }
    }
}

3.判断一个数是否质数

public class practice3 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个数:");
        int n = sc.nextInt();
        boolean b = isPrime(n);
        if (b){
   
            System.out.println(n + "是质数");
        }else {
   
            System.out.println(n + "不是质数");
        }
    }

    public static boolean isPrime(int n){
   
        for (int i = 2; i < (n / 2) + 1; i++){
   
            if (n % i == 0){
   
                return false;
            }
        }
        return true;
    }
}

4.打印

    *
   ***
  *****
 *******
*********
import java.util.Scanner;

public class practice4 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入打印的行数:");
        int n = sc.nextInt();
        print(n);
    }

    public static void print(int n){
   
        for (int i = 1; i <= n; i++){
   
            for (int j = n - i; j > 0; j--){
   
                System.out.print(" ");
            }
            for (int k = 1; k <= (2 * i) - 1; k++){
   
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

练习1:
输入三边的长度,求三角形的面积和周长(海伦公式)

import java.util.Scanner;

public class practice5 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一条边的长度:");
        double a = sc.nextInt();
        System.out.println("请输入第二条边的长度:");
        double b = sc.nextInt();
        System.out.println("请输入第三条边的长度:");
        double c = sc.nextInt();
        if (a + b > c && a + c > b && b + c > a){
   
            double area = area(a, b, c);
            System.out.println("面积为:" + area + ",周长为:" + (a + b + c));
        }else {
   
            System.out.println("这三条边无法组成三角形,请重新输入三边的长度!");

        }
    }

    public static double area(double a, double b, double c){
   
        double p = (a + b + c) / 2;
        return Math.sqrt(p * (p - a) * (p - b) * (p - c));
    }

}

练习2:
设计一个程序,完成(英雄)商品的购买
展示商品信息(折扣)->输入商品价格->输入购买数量->提示付款
输入付款金额->打印购买小票(扩展)

import java.util.Scanner;
public class practice6 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        System.out.println("苹果电脑(打9折)");
        System.out.println("请输入商品的价格:");
        double price = sc.nextDouble();
        System.out.println("请输入购买数量:");
        int num = sc.nextInt();
        double money = price * num * 0.9;
        System.out.println("应支付:" + money + 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值