栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA50道练习题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA50道练习题

写在前面:这一周在复习考试,复习间隙累了就看看简单的编程题目,当做熟悉java的基本知识

第一题

古典问题:有一对兔子,
从出生后第3个月起每个月都生一对兔子,
小兔子长到第三个月后每个月又生一对兔子,
假如兔子都不死,问每个月的兔子对数为多少?

import java.util.Scanner;//导入Scanner包,用来接受键盘的输入和输出



public class Question_one {
    public static void main(String[] args){
        System.out.println("请输入月份:");
        Scanner scan = new Scanner(System.in);//new一个Scanner的对象
        //Scanner scan= new Scanner(System.in);
        int n = scan.nextInt();//scan的读取int的方法
        System.out.println("第"+n+"个月兔子的数量为:"+func(n));
        scan.close();//结束,销毁scan这个类

    }
    private static int func(int n) {
        if (n == 1 || n == 2) {
            return 1;
        } else {
            return func(n - 1) + func(n - 2);
        }
    }

}

第二题

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

import java.util.Scanner;


public class Question_two {
    public static void main(String[] args){
        System.out.println("这是一个输出101-200之间有多少个素数的程序!");
        //System.out.println(func());
        int ans = 0;
        for(int j = 101; j <= 200; j++)
        {
            if(func(j) == 1)
            {
                System.out.print(j+" ");
                ans += 1;
            }
            if(ans == 10)
                System.out.println();
        }
        System.out.println();
        System.out.println("The total number is :"+ ans);
    }
    private static int func(int n)
    {
        int i = 0;
        for(i = 2; i < n / 2; i++){
            if(n % i == 0)
            {
                return 0;
            }
        }
        return 1;
    }
//    private static int func()
//    {
//        int ans = 0;
//        for(int i = 101; i <= 200; i++)
//        {
//            //System.out.println(i);
//            boolean flag = true;
//            for (int j = 2; j < i; j++)
//            {
//                if (i % j == 0)
//                {
//                    System.out.println("j is :"+j);
//                    flag = false;
//                }
//            }
//            if (flag == true)
//            {
//                System.out.println(i + 't');
//                ans += 1;
//            }
//        }
//        return ans;
//    }

}

第三题

题目:打印出所有的”水仙花数”,所谓”水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
例如:153是一个”水仙花数”,因为153=1的三次方+5的三次方+3的三次方

import java.lang.Math;


public class Question_three {
    public static void main(String[] args)
    {
        int ans = 0;
        for (int i = 100; i < 1000; i ++)
        {
            int o = i % 10;
            int p = ( i / 10) % 10;
            int q = i / 100;
            //if ( o * o * o + p * p * p + q * q * q == i )
            if( Math.pow(o ,3) + Math.pow(p, 3) + Math.pow(q, 3) == i)
            {
                System.out.println(i);
                ans += 1;
            }
        }
        System.out.println("There are " + ans + " Flower numbers!");
    }

}

第四题

题目:将一个正整数分解质因数。例如:输入90,打印出90=233*5。

import java.util.Scanner;


public class Question_four {
    public static void main(String[] args)
    {
        System.out.println("Please input a number: ");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        System.out.print(n + "=");
        int i = 2;
        while(i <= n)
        {
            while(n % i == 0 && n != i)  //关键点在于n != i这个条件,把握好这个条件才能输出正确
            {
                System.out.print(i + "*");
                n = n / i;
            }
            i += 1;
        }
        System.out.print(i - 1);
        
    }
}

第五题

题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示
条件运算符:也就是三目运算符

import java.util.Scanner;


public class Question_five {
    public static void main(String[] args)
    {
        System.out.println("Please input a number: ");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        System.out.println((n >= 90 ? 'A' : (n >= 60 ? 'B' : 'C')));

    }
}

第六题

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
辗转相除法

import java.util.Scanner;


public class Question_six {
    public static void main(String[] args)
    {
        System.out.println("Plz input two numbers m and n:");
        Scanner input = new Scanner(System.in);
        int m = input.nextInt();
        int n = input.nextInt();
        //朴素的思想解决最大公约数和最小公倍数问题
//        int i = m >= n ? n : m;
//        int k = m >= n ? m : n;
//        for(int j = i; j >= 1; j --)
//        {
//            if ( m % j == 0 && n % j == 0)
//            {
//                System.out.println("The max common divisor is : " + j);
//                break;
//            }
//        }
//        while(k % m != 0 || k % n != 0) //这个地方的判断条件用或,需要注意下
//        {
//            k++ ;
//        }
//        System.out.println("The min common mutiple is :" + k);
        //采用辗转相除法来求最大公约数,两个相除取余,对较小的那个数和余数重复这个操作,知道余数为零
        //求最小公倍数的方法:只需要两个数相乘,然后除以最大公约数即可
        int p = m;
        int q = n;
        while( q > 0)
        {
            int k = p % q;
            p = q;
            q = k;
        }
        System.out.println("The max common divisor is : " + p);
        System.out.println("The min common multiple is :" + m * n / p);


    }
}

第七题

题目:输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数。

import java.util.Scanner;


public class Question_seven {
    public static void main(String[] args)
    {
        System.out.println("Plz input a string: ");
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();//以换行分割输入,next是以空格分割的
        int Alpha_num = 0;
        int Space = 0;
        int num = 0;
        int other_letter = 0;
        //遍历字符串中的字符,三种方式-toCharArray()、charAt()、substring()
        char[] charArray = str.toCharArray();
        System.out.println(charArray);
        for(char i:charArray)
        {
            if(((int) i >= 65 &&  (int) i <= 90 ) || ((int) i >= 97 && (int) i <= 122))
                Alpha_num += 1;
            else if((int) i == 32)
                Space += 1;
            else if((int) i >= 48 && (int) i <= 57)
                num += 1;
            else
                other_letter += 1;
        }
        System.out.println("There are "+ Alpha_num + " Alphabet!");
        System.out.println("There are "+ num + " numbers!");
        System.out.println("There are "+ Space + " spaces!");
        System.out.println("There are " + other_letter + " other letters!");
    }

}

第八题

题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。
例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。输出结果的形式如:2+22+222=246;

import java.util.Scanner;


public class Question_eight {
    public static void main(String[] args)
    {
        System.out.println("Plz input number a:");
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        System.out.println("Plz input number n:");
        int n = input.nextInt();
        int sum = 0;
        int b = a;
        for (int i = 0; i < n - 1; i++)
        {
            sum += b;
            System.out.print(b + "+");
            b = a + b * 10;
        }
        System.out.print(b + "=");
        sum += b;
        System.out.print(sum);
    }


}

第九题

题目:一个数如果恰好等于它的因子之和,这个数就称为”完数”。例如6=1+2+3.编程找出1000以内的所有完数。


public class Question_nine {
    public static void main(String[] args)
    {
        int ans = 0;
        for(int i = 1; i <= 1000; i++)
        {
            int sum = 1;
            for(int j = 2; j < i / 2 +1; j ++)
            {
                if (i % j == 0)
                    sum += j;
            }
            if (sum == i) {
                ans += 1;
                System.out.println(i);
            }

        }
        System.out.println(ans);
    }
}

第十题

题目:一球从h米高度自由落下,每次落地后反跳回原高度的一半;
再落下,求它在 第n次落地时,共经过多少米?第n次反弹多高?

import java.util.Scanner;


public class Question_ten {
    public static void main(String[] args)
    {
        System.out.println("Plz input the height: ");
        Scanner input = new Scanner(System.in);
        float h = input.nextFloat();
        System.out.println("Plz input n: ");
        int n = input.nextInt();
        //第一次落地经过的距离为h
        //第一次弹起的高度为h/2
        //将第一次的拎出来单独计算,后面的统一
        float sum = h;
        h /= 2;
        for(int i = 2; i <= n; i++)
        {
            sum += 2 * h;
            h /= 2;
        }
        System.out.println("第" + n + "次反弹共经过" + sum + "米");
        System.out.println("第" + n + "次反弹" + h + "米");
        input.close();

    }

}

第十一题

题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
程序分析:可填在百位、十位、个位的数字都是1、2、3、4。这里要用3个for循环


public class Question_eleven {
    public static  void  main(String[] args)
    {
        int sum = 0;
        for (int i = 1; i <= 4; i ++)
        {
            for (int j = 1; j <= 4; j ++)
            {
                for(int k = 1; k <= 4; k ++)
                {
                    if( i != j && j != k && i != k)
                    {
                        System.out.println(i * 100 + j * 10 + k);
                        sum += 1;
                    }
                }
            }
        }
        System.out.println("answer is :" + sum);
    }
}

第十二题

题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;
利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;
20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;
60万到100万之间时,高于60万元的部分,可提成1.5%,
高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?

import java.util.Scanner;


public class Question_twelve {
    public static void main(String[] args)
    {
        System.out.println("Plz input profit: ");
        Scanner input = new Scanner(System.in);
        long I = input.nextLong();
        double bonus = 0;
        if( I <= 10)
            bonus += I * 0.1;
        else if( I <= 20)
            bonus += (I - 10) * 0.075 + 1;
        else if( I <= 40 )
            bonus += (I - 20) * 0.05 + 1.75;
        else if( I <= 60)
            bonus += ( I - 40) * 0.03 + 2.75;
        else if( I <= 100)
            bonus += ( I - 60) * 0.015 + 3.35;
        else
            bonus += ( I - 100) * 0.001 + 3.95;
        System.out.println("Bonus is :" + bonus);
    }
}

第十三题

题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

import java.lang.Math;


public class Question_thirteen{
    public static void main(String[] args)
    {
        for(int i = 1; i < 10000; i++)
        {
            int a = (int)Math.sqrt(i + 100);
            int b = (int)Math.sqrt(i + 168);
            //先开方,然后强制类型转换,在判断是否相等
            if( (a * a == (i + 100)) && (b * b == (i + 168)))
                System.out.println(i);
        }
    }
}

第十四题

题目:输入某年某月某日,判断这一天是这一年的第几天?

import java.util.Scanner;


public class Question_fourteen {
    public static void main(String[] args)
    {
        System.out.println("请输入年月日,用空格隔开:");
        Scanner input = new Scanner(System.in);
        int year = input.nextInt();
        int month = input.nextInt();
        int day = input.nextInt();
        //学习如何初始化数组
        int[] arr ={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        //判断闰年且月份大于2才能多加一
        boolean flag = (((year % 4 ==0 && year % 100 != 0) || (year % 400) == 0) && month > 2);
        int sum = day;
        for(int i = 0; i < month - 1; i ++)
            sum += arr[i];
        if( flag )
        {
            sum += 1;
        }
        System.out.println("输入的是" + year + "年第" + sum + "天!" );
        input.close();
    }
}

第十五题

题目:输入三个整数x,y,z,请把这三个数由小到大输出。

import java.util.Scanner;


public class Question_fifteen {
    public static void  main(String[] args)
    {
        System.out.println("Plz input x, y, z:");
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int y = input.nextInt();
        int z = input.nextInt();
        //两两进行比较,设x为最大,y为中间,z为最小,不符合这个顺序就交换
        //因为重复的比较多,可以抽象成为一个函数
        if( x < y)
        {
            int tmp = x;
            x = y;
            y =tmp;
        }
        if( x < z)
        {
            int tmp = x;
            x = z;
            z = tmp;
        }
        if(y < z)
        {
            int tmp = y;
            y = z;
            z = tmp;
        }
//        if(x < y)
//            swap(x, y);
//        if(x < z)
//            swap(x, z);
//        if(y < z)
//            swap(y, z);
        //java的控制输出
        //用到的是printf这个方法,用println会报格式错误
        System.out.printf("从大到小的顺序为%d %d %d",x, y , z);
        input.close();
    }
    //写这个函数的时候,发现java 只有传值,没有传引用,java也不能同时return两个值,下面这个方法就不行了
//    private static void swap(int x,int y)
//    {
//        int tmp = x;
//        x = y;
//        y = tmp;
//        return x, y;
//    }
}

第十六题

题目:输出9*9口诀。



public class Question_sixteen {
    public static void main(String[] args)
    {
        for(int i = 1; i <= 9; i++)
        {
            for(int j = 1; j <= i; j++)
            {
                System.out.printf("%d X %d = %2d ", i, j, i * j);//格式输出的问题和对其的问题
            }
            System.out.println();
        }
    }
}

第十七题

​ 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
以后每天早上都吃了前一天剩下的一半零一个。
到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。


public class Question_seventeen {
    public static void main(String[] args)
    {
        int count = 1;
        for(int i = 0; i < 10; i++)
        {
            count = (count + 1) * 2;
        }
        System.out.println("total apple is :" + count);
    }
}

第十八题

题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。
已抽签决定比赛名单。
有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。


public class Question_eighteen {
    public static void main(String[] args)
    {
        //涉及到怎么初始化数组
        char[] m = {'a', 'b', 'c'};
        char[] n = {'x', 'y', 'z'};
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                //下面的判断条件经过自己的处理之后得到的
                if(m[i] == 'a' && n[j] == 'x' || m[i] == 'a' && n[j] == 'y')
                    continue;
                else if(m[i] == 'b' && n[j] == 'x' || m[i] == 'b' && n[j] == 'z' )
                    continue;
                else if(m[i] == 'c' && n[j] == 'x' || m[i] == 'c' && n[j] == 'y')
                    continue;
                else
                    System.out.println(m[i] + " VS " + n[j]);

            }
        }
    }
}

第十九题

打印菱形

​ *
***
*****
*******
*****
***
​ *

进程已结束,退出代码0

public class Question_nineteen {
    public static void main(String[] args) {
        //打印上面部分:
        int n=4;
        for (int i = 0; i < n; i++) {  //控制行
            for (int k =3-i; k > 0 ; k--) { //控制*前面空格的输出
                System.out.print(" ");
            }
            //输出符号,但不要换行
            for (int j = 0; j <=2*i;j++) {  //控制列
                System.out.print("*");
            }
            //输出完符号马上换行
            System.out.println();
        }
        //打印下面部分
        n=3;
        for (int i = n; i > 0; i--) {  //控制行
            for (int k =3-i+1; k > 0 ; k--) { //控制*前面空格的输出,第一行要加空格
                System.out.print(" ");
            }
            //输出符号,但不要换行
            for (int j = 0; j <=2*i-2;j++) {  //控制列
                System.out.print("*");
            }
            //输出完符号马上换行
            System.out.println();
        }

    }
}

第二十题

题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和


public class Question_twenty {
    public static void main(String[] args)
    {
        float up = 2;
        float down = 1;
        float fraction = up / down;
        for(int i = 0; i < 19; i++)
        {
            float tmp = up + down;  //这个地方要引入一个临时变量,当做中间变量交换
            fraction += tmp / up;
            down = up;
            up = tmp;

        }
        System.out.println("sum is " +fraction);
    }
}

未完,待补充……

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/762704.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号