java入门实例题型(冒泡排序,回文数,阶乘,菲波纳锲数列)

本文提供了一系列Java入门实例,包括斐波纳契数列(递归与非递归实现)、素数判断、回文数检查、阶乘计算、阿姆斯壮数检测以及冒泡排序和选择排序算法的实现。通过这些实例,读者可以深入理解Java编程基础和常见算法的应用。

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

java入门实例题型

  1. 斐波纳契数列
    编写一个java程序打印斐波纳契(fibonacci)数列不使用递归和使用递归。
    输入: 8
    输出: 0 1 1 2 3 5 8 13
public class ex {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        for (int i = 0;i < x ;i++){
            System.out.print(fi(i)+" ");
        }
    }
    public static int fi (int x){
        if( x == 0 || x == 1){
            return x;
        }
        else
            return fi(x-1) + fi(x - 2);
    }

结果

8
0 1 1 2 3 5 8 13 
Process finished with exit code 0

  1. 素数实例编写一个java程序来判定给定的一个数字是否为素数。
    输入: 12
    输出:false
    输入: 11
    输出:true
public class ex {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        boolean isprime = true;
        int n = (int)Math.sqrt(x);
        for ( int i = 2;i <= n;i++){
            if(x % i == 0)
                isprime = false;
        }
        System.out.println(isprime);

    }
}

结果

11
true

Process finished with exit code 0

12
false

Process finished with exit code 0
  1. 回文数编写一个java程序断定给定的字符串是否为一个回文数。
    输入: 321
    输出: false
    输入: 12344321
    输出: true
public class ex {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int n = x;
        int sum = 0,m;
        boolean ishuiwen = true;
        while(x != 0){
            m = x % 10;
            sum = sum * 10 + m;
            x /= 10;
        }
        if (sum != n)
            ishuiwen = false;

        System.out.println(ishuiwen);

    }
}

结果

321
false

Process finished with exit code 0

12344321
true

Process finished with exit code 0

  1. 阶乘编写一个java程序来打印数字的阶乘。
    输入: 5
    输出: 120
public class ex {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        System.out.println(fac(x));

    }
    public static int fac(int x){
        if(x == 1 || x == 0)
            return 1;
        else
            return x * fac(x - 1);
    }
}

结果

5
120

Process finished with exit code 0

  1. 阿姆斯壮数
    编写一个java程序来断定一个数值是否为阿姆斯壮(Armstrong)数。
    输入: 153
    输出: true
    输入: 12
    输出: false
public class ex {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int n,sum = 0,y = x;
        boolean isAmusi = false;
        while (x != 0){
            n = x % 10;
            sum += n*n*n;
            x /= 10;
        }
        if(sum == y){
            isAmusi = true;
        }
        System.out.println(isAmusi);
    }
}

结果

153
true

Process finished with exit code 0

12
false

Process finished with exit code 0

  1. 冒泡排序
    编写一个java程序,使用冒泡排序算法对数组元素进行排序。
    排序前:1,5,6,8,9,2
    排序后:1,2,5,6,8,9
public class ex {
    public static void main(String[] args) {
        int  a []={2,3,4,1,6,7};
        int tem;
        for(int i = 1;i <= a.length ;i++){
            for(int j = 1;j <=a.length - i;j++){
                if(a[j - 1] > a[j]){
                    tem = a[j-1];
                    a[j-1] = a[j];
                    a[j] = tem;
                }
            }
        }
        for(int i : a){
            System.out.print(i+" ");
        }
    }
}

1 2 3 4 6 7 
Process finished with exit code 0

7,选择排序
编写一个java程序,使用选择排序算法对数组元素进行排序。
排序前:1,5,6,8,9,2
排序后:1,2,5,6,8,9

public class ex {
    public static void main(String[] args) {
        int  a []={2,3,4,1,6,7};
        int tem,index,smaller;
        for(int i = 0;i < a.length;i++){
            index = i;
            for(int j = i+1;j < a.length;j++){//内循环是为了找出最小数字的索引
                if(a[j] < a[index]){
                    index = j;
                }
            }
            smaller = a[index];
            a[index] = a[i];
            a[i] = smaller;
        }
        for(int i : a){
            System.out.print(i+" ");
        }
    }
}

结果

1 2 5 6 8 9 
Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值