Java超详细的基础编程300题,附带答案,持续更新中~

目录

1.输出"Java是世界上最好的语言!"。开始你的编程之旅吧。

2.计算带余除法

3.整数的个位

4.整数的十位

5.计算两个整数的和

6.求两个整数中的最大值

7.判断素数

8.判断闰年

9.输出素数

10.输出闰年

11.数字9出现的次数

12.统计成绩

13.温度转换

14.圆的面积

15.张三喝水

16.张三排电梯

17.张三的朋友与欧几里得


1.输出"Java是世界上最好的语言!"。开始你的编程之旅吧。

题目描述:无

public class eat {
    public static void main(String[] args) {
        System.out.println("Java是世界上最好的语言!");
    }
}

2.计算带余除法

题目描述:给定两个整数a和b (0 < a,b < 10,000),计算a除以b的整数商和余数。

public class eat {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //输入
        int a = input.nextInt();
        int b = input.nextInt();
        System.out.println("商:" + a / b);//商
        System.out.println("余数:" + a % b);//余数
    }
}

3.整数的个位

题目描述:输入一个整数a, 求个位数是几

public class Array {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();//输入
        if (a < 9) {//只有一位数的情况本身就是个位
            System.out.println(a);
        }else {
            System.out.println(a % 10);//模10求余数
        }
    }
}

4.整数的十位

题目描述:输入一个整数a, 求十位数是几

public class Array {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();//输入
        if (a < 9) {//只有一位数的情况本身就是十位
            System.out.println(a);
        }else {
            System.out.println(a / 10);//除10求余数
        }
    }
}

5.计算两个整数的和

题目描述:输入两个整数,计算它们两个的和

public class Array {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入两个整数:");
        //输入
        int x = input.nextInt();
        int y = input.nextInt();
        //计算和
        int sum = x + y;
        System.out.println(sum);//输入两个整数和
    }
}

6.求两个整数中的最大值

题目描述:输入两个整数求的最大值

方法1:if语句判断

public class Array {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入两个整数:");
        //输入
        int x = input.nextInt();
        int y = input.nextInt();
        //计算最大值
        int max = 0;//存储最大值的变量
        if (x > y) {
            max = x;
        }else {
            max = y;
        }
        System.out.println(max);//输入最大值
    }
}

方法2:条件表达式

public class Array {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入两个整数:");
        //输入
        int x = input.nextInt();
        int y = input.nextInt();
        //计算最大值
        int max = x > y ? x : y;//存储最大值的变量
        System.out.println(max);//输入最大值
    }
}

7.判断素数

题目描述:输入一个整数,判断是不是素数

public class Array {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入1个整数:");
        //输入
        int num = input.nextInt();
        //素数:不能被除了1和它本身的数整除
        for (int i = 2; i < num; i++) {
            if (num % i == 0) {
                System.out.println("不是素数");
                break;
            }else {
                System.out.println("是素数");
            }
        }
    }
}

8.判断闰年

题目描述:输入一个年份,判断其是不是闰年

public class Exercise {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //输入
        int year = input.nextInt();
        //闰年:能被4整除并且不能被100整除 - 或者能被400整除
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            System.out.println("Is leap year");//是闰年
        }else {
            System.out.println("Is not leap year");//不是闰年
        }
    }
}

9.输出素数

题目描述:打印100~200的素数

public class Exercise {
    public static void main(String[] args) {
        for (int i = 100; i <= 200 ; i++) {
            for (int j = i + 1; j < 200 ; j++) {
                if (i % j == 0) {
                    //不是素数 - 不需要打印
                }
            }
            //程序走到这里说明内循环走完了都不能整除 - 是素数输出
            System.out.print(i + " ");
        }
    }
}

10.输出闰年

题目描述:打印1000~2000的闰年

public class Exercise {
    public static void main(String[] args) {
        for (int i = 1000; i <= 2000 ; i++) {
            //利用循环一个数一个数判断
            if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
                System.out.println(i);
            }
            //不是的话这不需要输出
        }
    }
}

11.数字9出现的次数

题目描述:统计1~100之间出现了几次数字9

public class Exercise {
    public static void main(String[] args) {
        int count = 0;//记录9的个数
        for (int i = 1; i <= 100 ; i++) {
            if (i % 10 == 9) {
                //看个位是不是9
                count++;
            }else if (i / 10 == 9) {
                //看十位是不是9
                count++;
            }
        }
        System.out.println(count);
    }
}

12.统计成绩

题目描述:统计十个同学的最高分,最低分以及平均分。

public class Exercise {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double max = 0.0;//最高分
        double min = 100.0;//最低分
        double mean = 0.0;//平均值
        double sum = 0.0;//求和
        double[] score = {77.1, 88.1, 76.1, 56.1, 87.1, 98.1, 55.1, 94.1, 39.1, 96.1};
        //求和
        for (int i = 1; i <= score.length - 1 ; i++) {
            sum += score[i];//求数组元素值的和
        }
        for (int i = 1; i <= score.length - 1 ; i++) {
            if (score[i] > max) {//此时i下标大于此时的max,i下标的值就是此时的最高分
                max = score[i];
            }
            if (score[i] < min) {//此时i下标小于此时的max,i下标的值就是此时的最低分
                min = score[i];
            }
        }
        mean = sum / score.length;//计算平均数
        System.out.println(max);
        System.out.println(min);
        System.out.println(mean);
    }
}

13.温度转换

题目描述:输入一个浮点数f, 表示华氏温度, 输出对应的摄氏温度c , c=5/9*(f-32)

public class Exercise {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double F = input.nextDouble();//华氏温度
        double C = input.nextDouble();//摄氏温度
        C = ((F - 32) * 5) / 9;//通过公式转化成摄氏温度
        System.out.println(C);//输出摄氏温度
    }
}

14.圆的面积

题目描述:有一个半径为 r 的圆,请你计算这个圆的面积。圆的面积公式是π r^2 ,其中 π  取 3.14

public class Exercise {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int r = input.nextInt();
        double s = 0.0;//统计面积
        s = 3.14 * (r * r);
        System.out.println(s);

    }
}

15.张三喝水

题目描述:现在有人口渴了,要喝10升水才能解渴,但现在只有一个深 h 厘米,底面半径是 r 厘米的水杯,这个人最少要喝多少杯水才能解渴。

其中 π 取 3.14 ,h  和 r  是整数。

public class Exercise {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double r = input.nextDouble();
        double h = input.nextDouble();
        double sum = 0.0;
        double v = 0.0;
        System.out.println((int) (10000 / (3.14 * h * r * r)) + 1);
    }
}

16.张三排电梯

题目描述:张三学校教学楼的电梯前排了很多人,他的前面有n个人在等电梯。电梯每次可以乘坐12人,每次上下需要的时间为4分钟(上需要2分钟,下需要2分钟)。请帮助张三计算还需要多少分钟才能乘电梯到达楼上。(假设最初电梯在1层)

public class Exercise {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int k = 0;
        int z = 0;
        if (n > 12) {  //n大于12说明电梯一次运不完
            if (n % 12 == 0) {  //n % 12等于0说明电梯装满人了并且没有剩下人
                z = n / 12;  //将n除12的值赋给z,得到坐电梯的次数
                k = z * 4;   //电梯每次上和下用掉的时间*做电梯的次数就是上楼的时间
            } else {      //还有人没有运完
                z = n / 12;//坐电梯的次数
                k = z * 4 + 2;//因为还有人没有运完,所以电梯还要再运上去一次,也就是加两分钟
            }
        } else if (n == 12) {  //n等于12说明电梯一次正好可以运完
            k = 4 + 2;//将前面的人运完后再运小乐乐,也就是4+2分钟
        }
        else{ //n小于12说明电梯在一次可以运完的情况下,而且没有装满,所以小乐乐可以跟着上去
            // k = 2;//k就是小乐乐上楼的时间
        }
        System.out.println(k);//上楼的时间
    }
}

17.张三的朋友与欧几里得

题目描述:李四最近在课上学习了如何求两个正整数的最大公约数与最小公倍数,但是他竟然不会求两个正整数的最大公约数与最小公倍数之和,请你帮助他解决这个问题。

public class Exercise {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        long  a = input.nextLong();
        long  b = input.nextLong();
        long comax = 0;
        long comin = 0;
        long k = a * b;
        while (a != 0 && b != 0) {
            if (a > b) {
                a = a % b;
            }
            else {
                b = b % a;
            }
        }
        comax = a > b ? a : b;
        comin = k / comax;
        System.out.println(comax + comin);
    }
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>