JAVA初阶简单的代码题目(1)

1.用JAVA求解汉诺塔问题 

兄弟们第一题直接上难度,如果看不懂可以点链接看视频详解https://www.bilibili.com/video/BV1yT4y1v7JV/?spm_id_from=333.337.search-card.all.click&vd_source=26d9c6d955ad2155188d2ce4ea5646fc

public class HanoiTower {
        public void main(String[] args) {
            Tower tower = new Tower(5);
            tower.move(5, 'A', 'B', 'C');
        }
    }

    class Tower {
        private int num;
        private HashMap<String, String[]> tower = new HashMap<>();
        private String emptyLine = "";
        public Tower(int num) {
            this.num = num;
            initialTower("A");
            initialTower("B");
            initialTower("C");
            initialEmptyLine();
            printTower();
        }
        public void initialTower(String name) {
            String[] s = new String[num];
            for (int i=0; i<num; i++) {
                s[i] = "";
            }
            for (int i=0; i<num; i++) {
                if ( name.equals("A")) {
                    int j;
                    for (j=0; j<i+1; j++) {
                        s[i] += "#";
                    }
                    for (int k=0; k<num-j; k++) {
                        s[i] += "-";
                    }
                } else {
                    for (int j=0; j<num; j++) {
                        s[i] += "-";
                    }
                }
            }
            tower.put(name, s);
        }
        public void initialEmptyLine() {
            for (int i=0; i<num; i++) {
                emptyLine += "-";
            }
        }
        public void moveTower(String currentTower, String targetTower) {
            String transfer = "";
            String[] currentString = tower.get(currentTower);
            for (int i=0; i<num; i++) {
                if (currentString[i].indexOf("#") != -1) {
                    transfer = currentString[i];
                    currentString[i] = emptyLine;
                    break;
                }
            }
            String[] targetString = tower.get(targetTower);
            for (int i=num-1; i>=0; i--)
                if (targetString[i].indexOf("#") == -1) {
                    targetString[i] = transfer;
                    break;
                }
        }
        public void printTower() {
            String[] towerA = tower.get("A");
            String[] towerB = tower.get("B");
            String[] towerC = tower.get("C");
            for (int i=0; i<num; i++) {
                System.out.println(towerA[i] + " " + towerB[i] + " " + towerC[i]);
            }
        }
        public void move(int num, char current, char transfer, char target) {
            if (num == 1) {
                System.out.println(current + "->" + target);
                moveTower(current + "", target + "");
                printTower();
            } else {
                move(num - 1, current, target, transfer);
                System.out.println(current + "->" + target);
                moveTower(current + "", target + "");
                printTower();
                move(num - 1, transfer, current, target);
            }
        }
    }

 

2.如何判断素数

public class Test {
   public static void main(String[] args) {
        int n=7;
        int i=2;
        for (; i <=Math.sqrt(n); i++) {    
//通常数学中的公式我们可以用Math来使用,此处为求根号
            if( n % i == 0){
                break;
            }
        }
        if( i>Math.sqrt(n)) {
            System.out.println(n+"是素数");
        }
    }
}

3.出现9的次数

public class Test {

    public static void main(String[] args) {
        int count=0;
        for (int i = 0; i <=100; i++) {
            if(i/10==9 || i%10==9){
                count ++;
            }
        }
        System.out.println(count);

    }
}

4.水仙花数

要求:求出0~n之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本 身,如;153=1^3+5^3+3^3,则153是一个“水仙花数“。)

public class Test {
   public static void main(String[] args) {
        for (int i = 0; i < 99999; i++) {
            int count=0;
            int tem=i;
            while(tem!=0){
                count++;
                tem/=10;
            }
            tem=i;
            int sum =0;
            while(tem!=0){
                sum+=Math.pow(tem%10,count);//谁的多少次方可以用Math.pow()来计算
                tem/=10;
            }
            if(sum==i){
                System.out.println(i);
            }
        }
    }
}

5.计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值

public class Test {
   public static void main(String[] args) {
        double sum=0;
        int flag=1;         //flag的作用是让下一个相加的数字变成它的负数
        for (int i = 1; i <=100 ; i++) {
            sum+=1.0/i*flag;
            flag=-flag;
        }
        System.out.println(sum);
    }
}

6.给定两个数,求这两个数的最大公约数

要求:输入:20 40
           输出:20
public class Test {
   public static void main(String[] args) {
        Scanner Scanner=new Scanner(System.in);
        int a= Scanner.nextInt();
        int b= Scanner.nextInt();
        int c= a % b;
        while (c != 0) {
            a=b;
            b=c;
            c=a%b;
        }
        System.out.println(b);
    }
}

7.二进制1的个数。

内容:求一个整数,在内存当中存储时,二进制1的个数。

public class Test {
   public static void main(String[] args) {
        int n=7;
        int count=0;
        while(n!=0){
            n=n&(n-1);
            count++;
        }
        System.out.println(count);
    }
}

8.使用函数求最大值

要求:创建方法求两个数的最大值max1,随后再写一个求3个数的最大值的函数max2。在max2这个函数中,调用max1函数,来实现3个数的最大值计算

public class Test {
     public static int max1(int a,int b){
            return a>b ? a:b;
        }
        public static int max2(int a,int b,int c) {
            int max = max1(a, b);
            int ret = max1(max, c);
            return ret;
        }

    public static void main(String[] args) {
        System.out.println(max1(2, 3));
        System.out.println(max2(1, 2, 3));
    }
}

9.求和的重载

要求:在同一个类中,分别定义求两个整数的方法 和 三个小数之和的方法。 并执行代码,求出结果。

public class Test {
    public static int add(int a,int b){
            return a+b;
        }
        public static double add(double a,double b,double c) {
        return a+b+c;
        }

    public static void main(String[] args) {
        System.out.println(add(3, 4));
        System.out.println(add(3.1, 3.2, 3.3));

    }
}

10.求最大值方法的重载

要求:在同一个类中定义多个方法:要求不仅可以求2个整数的最大值,还可以求3个小数的最大值?

public class Test {
    public static double max1(double a,double b){
            return a>b ? a:b;
        }
        public static double max1(double a,double b,double c) {
            double max = max1(a, b);
            double ret = max1(max, c);
            return ret;
        }

    public static void main(String[] args) {
        System.out.println(max1(2.1, 3.2));
        System.out.println(max1(1.3, 2.7, 3.6));
    }
}

11.奇数位于偶数之前

要求:调整数组顺序使得奇数位于偶数之前。调整之后,不关心大小顺序。

 public class Test {
   public static void fun(int[] array){
        int left =0;
        int right =array.length-1;
        while(left<right) {
            while (left < right && array[left] % 2 == 0) {
                left++;
            }
           while (left < right && array[right] % 2 == 0){
                right--;
            }
        }
        swap(array,left,right);
    }
    public static void swap(int [] array,int i,int j){
        int tem=array[i];
        array[i]=array[j];
        array[j]=tem;
    }
    public static void main(String[] args) {
        int[] array={1,2,3,4,5,6,7,8,9};
        fun(array);
        System.out.println(Arrays.toString(array));
    }
}

12.改变原有数组元素的值

要求:实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素 乘以 2 , 并设置到对应的数组元素上. 例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}

public class Test {
    public static void main(String[] args) {
        int[] array0={1,2,3};
        transform(array0);
    }
    public static void transform(int[] array){
        for (int x :array){
            System.out.print(x*2+" ");
        }
    }
}

13.打印X图形

import java.util.Scanner;

    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextInt()) { 
                int a = in.nextInt();
                for(int i=0;i<a;i++){
                    for(int j=0;j<a;j++){
                        if(i==j || i+j==a-1){
                            System.out.print("*");
                        }else{
                            System.out.print(" ");
                        }
                    }
                    System.out.println();
                }
            }
        }
    }
}

 

 希望这些简单JAVA代码题目可以帮助到你,这只是一部分题型,请大家敬请期待!!!!!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值