Java50道编程题

这篇博客包含了50道Java编程题目,包括条件运算符应用、最大公约数和最小公倍数计算、字符统计、数字串求和、完数判断、自由落体问题以及无重复数字的三位数组合等。通过这些题目,可以锻炼和提升Java编程技能。

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

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

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个成绩:");
        int r = sc.nextInt();
        char grade = r>=90?'A':r>=60?'B':'C';
        System.out.println("等级为:"+grade);
    }
}

题目6:输入两个正整数m和n,求其最大公约数和最小公倍数。
思路:先求最大公约数,再求最小公倍数。
求最大公约数:欧几里德算法 gcd(a,b) = gcd(b,a mod b)。
最小公倍数:a*b /gcd(a,b)

//1.求最大公约数
public static int gcd(int p,int q){
    if(q==0){
        return p;
    }
    int r = p%q;
    return gcd(q,r);
}
2.求最小公倍数
 public static int lcm(int a,int b){
    return a*b/gcd(a,b);
 }
 public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);
     int i = sc.nextInt();
     int i1 = sc.nextInt();
     int gcd = gcd(i, i1);
     int lcm = lcm(i, i1);
     System.out.println("最大公约数:"+gcd+"最小公倍数:"+lcm);
 }

在这里插入图片描述
题目7:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

public class Solution {
public static void main(String[] args) {
    int digital=0;
    int character =0;
    int blank =0;
    int others =0;
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    char[] chars = s.toCharArray();
    int len = chars.length;
    for (int i = 0; i < len; i++) {
        if(chars[i]>='0'&&chars[i]<='9'){
            digital++;
        }else if(chars[i]>='a'&&chars[i]<='z'||chars[i]>='A'&&chars[i]<='Z'){
            character++;
        }else if(chars[i]==' '){
            blank++;
        }else{
            others++;
        }
    }
    System.out.println("========================");
    System.out.println("英文字母的个数"+character);
    System.out.println("空格的个数"+blank);
    System.out.println("数字的个数"+digital);
    System.out.println("其它字符的个数"+others);
}
}

结果为:
在这里插入图片描述

题目8
求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。再输入5例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

public static void main(String[] args) {
    long a,b =0,sum=0;
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入数字a的值:");
    a = sc.nextInt();
    System.out.println("输入相加的项数:");
    int n = sc.nextInt();
    int i=0;
    while(i<n){
        b=b+a;
        sum=sum+b;
        a=a*10;
        ++i;
        System.out.println(b);
    }
    System.out.println(sum);
}

在这里插入图片描述

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

public class Main {
public static void main(String[] args) {
    System.out.println("1到1000的所有的完数");
    for (int i = 1; i < 1000; i++) {
        int t=0;
        for (int j = 1; j <= i / 2; j++) {
            if(i%j==0){
                t=t+j;
            }
        }
        //6+1+2+3
        if(t==i){
            System.out.print(i+" ");
        }
    }
}
}

在这里插入图片描述

题目10

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

public class Solution {
public static void main(String[] args) {
    int h=100; int s=0;
    for (int i = 1; i < 10; i++) {
        s= s + h;
        h=h/2;
    }
    System.out.println("共经过"+s+"米");
    System.out.println("第10次反弹多高?"+h);
}

在这里插入图片描述

题目11

有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

public class Main {
public static void main(String[] args) {
    int count = 0;
    for (int i = 1; i < 5; i++) {
        for (int j = 1; j < 5; j++) {
            for (int k = 1; k < 5; k++) {
                if (i != j && j != k && k != i) {
                    count++;
                    System.out.print(i * 100 + j * 10 + k + " ");
                }
            }
        }
        System.out.println();
    }
    System.out.println("共有" + count + "个三位数");
}
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值