Java编程题练习

这篇博客主要介绍了16个Java编程题目,涵盖了从基础到进阶的各类问题,旨在提升读者的Java编程技能。

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

1.

package Test;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        System.out.println("请输入一个点的坐标:");
        Scanner sc = new Scanner(System.in);
        double x = sc.nextDouble();
        double y = sc.nextDouble();
        System.out.println(Pan(x,y));
    }

    public static boolean Pan(double a,double b){
        if (a <= 200 && b <= (-0.5 * a  + 100)){
            return true;
        }
        return false;
    }
}

/*
请输入一个点的坐标:
200 1
false
*/

2.

package Test;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        System.out.println("请输入第一个点的坐标以及矩形的宽高:");
        Scanner sc = new Scanner(System.in);
        double x1 = sc.nextDouble();
        double y1 = sc.nextDouble();
        double w1 = sc.nextDouble();
        double h1 = sc.nextDouble();
        System.out.println("请输入第二个点的坐标及矩形的宽高:");
        Scanner sc1 = new Scanner(System.in);
        double x2 = sc1.nextDouble();
        double y2 = sc1.nextDouble();
        double w2 = sc1.nextDouble();
        double h2 = sc1.nextDouble();

        double x = x1 -x2 >=0 ? x1-x2 : x2-x1;
        double y = y1-y2 >=0? y1-y2 : y2-y1;

        if (x <= (w1 - w2) / 2 && y <= (h1 - h2) / 2){
            System.out.println("r2 is inside r1");
        }
        else if (x<= (w1 + w2) / 2 && y <= (h1 + h2) / 2){
            System.out.println("r2 overlaps r1");
        }
        else {
            System.out.println("r2 does not overlap r1");
        }
    }
}

 3.

package Test;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        System.out.println("请输入第一个圆的中心和半径:");
        Scanner sc = new Scanner(System.in);
        double x1 = sc.nextDouble();
        double y1 = sc.nextDouble();
        double b1 = sc.nextDouble();
        System.out.println("请输入第二个圆的中心和半径:");
        Scanner sc1 = new Scanner(System.in);
        double x2 = sc1.nextDouble();
        double y2 = sc1.nextDouble();
        double b2 = sc1.nextDouble();

        if (Math.sqrt((x1 - x2) +(y1 - y2)) <= Math.abs(b1 - b2)){
            System.out.println("圆二在圆一内");
        }
        else if (Math.sqrt((x1 - x2) +(y1 - y2)) <= (b1 + b2)){
            System.out.println("两个圆重叠!");
        }
        else {
            System.out.println("两个圆不相交");
        }
    }
}


/*
请输入第一个圆的中心和半径:
0 0 1
请输入第二个圆的中心和半径:
10 10 1
两个圆不相交
*/

4.

package Test;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        System.out.println("请输入一个整数:");
        Scanner sc = new Scanner(System.in);
        int x1 = sc.nextInt();
        Pan(x1);
    }

    public static void Pan(int userInt){
        boolean bool_5 = (userInt % 5 == 0) ? true : false;
        boolean bool_6 = (userInt % 6 == 0) ? true : false;
        boolean bool = ((bool_5==true && bool_6==false) || (bool_5==false && bool_6==true)) ? true : false;
        System.out.println("Is "+ userInt + " divisible by 5 and 6? " +
                (bool_5 && bool_6));
        System.out.println("Is "+ userInt + " divisible by 5 or 6? " +
                (bool_5 || bool_6));
        System.out.println("Is "+ userInt + " divisible by 5 or 6, but not both?  " + bool);
    }
}


/*
请输入一个整数:
30
Is 30 divisible by 5 and 6? true
Is 30 divisible by 5 or 6? true
Is 30 divisible by 5 or 6, but not both?  false
*/

5.

package Test;

import java.util.Random;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Cai();
    }

    public static void Cai() {
        Random random = new Random();
        int zhen = random.nextInt(100);
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("请输入您猜的数字:");
            int usr = input.nextInt();
            if (usr > zhen) {
                System.out.println("猜大了!");
            } else if (usr < zhen) {
                System.out.println("猜小了!");
            } else {
                System.out.println("回答正确!正确数字为:" + zhen);
                break;
            }
        }
    }
}


/*
请输入您猜的数字:
50
猜小了!
请输入您猜的数字:
75
猜小了!
请输入您猜的数字:
85
猜大了!
请输入您猜的数字:
80
猜大了!
请输入您猜的数字:
78
猜大了!
请输入您猜的数字:
76
回答正确!正确数字为:76

*/

6.

package Test;

public class Test {
    public static void main(String[] args) {
        System.out.println(Z(15,25));
    }

    public static int Z(int a,int b){
            int gcd = 1;
            int n = a > b ? a : b;
            for (int i = 1; i < n; i ++){
                if ( a % i == 0 && b % i == 0){
                    gcd = i;
                }
            }
            return gcd;
    }
}




/*
5
*/

7.

package day7_27;

import java.util.Scanner;

public class Zuo56 {
	public static void main(String[] args) {
		System.out.print("请输入五边形的半径:");
		Scanner scanner = new Scanner(System.in);
		double r = scanner.nextDouble();
		double s = 2 * r * Math.sin(Math.PI / 5);
		double m = (5 * s * s) / (4 * Math.tan(Math.PI / 5));
		System.out.println("此五边形的面积为:"+ m);
		scanner.close();
	}
}


/*
请输入五边形的半径:1.5
此五边形的面积为:5.349692904160238
*/

 8.

package day7_27;

import java.util.Scanner;

public class Zuo58 {
	public static void main(String[] args) {
		System.out.println("请输入(0~127)之间的ASCII码,你会得到相应的字符:");
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		char z = (char)a;
		scanner.close();
		System.out.println("对应的字符为:" + z);
	}
}


/*
请输入(0~127)之间的ASCII码,你会得到相应的字符:
69
对应的字符为:E
*/

9.

package day7_27;

import java.util.Scanner;

public class Zuo59 {
	public static void main(String[] args) {
		System.out.println("请输入一个整数(0~15):");
		Scanner scanner = new Scanner(System.in);
		int s = scanner.nextInt();
		if (s < 0 || s > 15) {
			System.out.println("输入数字不合法!");
		}else if(s <= 9){
			System.out.println("对应的十六进制为:" + s);
		}else {
			switch (s % 10) {
			case 0:
				System.out.println("对应的十六进制为:A");
				break;
			case 1:
				System.out.println("对应的十六进制为:B");
				break;
			case 2:
				System.out.println("对应的十六进制为:C");
				break;
			case 3:
				System.out.println("对应的十六进制为:D");
				break;
			case 4:
				System.out.println("对应的十六进制为:E");
				break;
			case 5:
				System.out.println("对应的十六进制为:F");
				break;
			}
		}
		scanner.close();
	}
}

/*
请输入一个整数(0~15):
-1
输入数字不合法!
*/

10.

package day7_27;

import java.util.Scanner;

public class Zuo60 {
	public static void main(String[] args) {
		System.out.print("Enter string s1:");
		Scanner scanner = new Scanner(System.in);
		String s1 = scanner.nextLine();
		System.out.print("Enter string s2:");
		Scanner scanner1 = new Scanner(System.in);
		String s2 = scanner1.nextLine();
		scanner.close();
		scanner1.close();
		Boolean fLAGBoolean =false;
		for (int i = 0; i < s1.length(); i ++) {
			if (s1.charAt(i) == s2.charAt(0)) {
				if (s1.charAt(i + 1) == s2.charAt(1)) {
					fLAGBoolean = true;
				}
			}
		}
		if (fLAGBoolean) {
				System.out.println(s1 + " is a substring of " + s2);
		}else {
			System.out.println(s1 + " is not a substring of " + s2);
		}
	}
}

/*
Enter string s1:ads
Enter string s2:df
ads is not a substring of df
*/

 11.

package day7_27;

import java.util.Scanner;

public class Zuo61 {
	public static void main(String[] args) {
		System.out.print("请输入一个社保卡号码:");
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();
		String fstr[] = str.split("-");
		if (fstr[0].length() == 3 && fstr[1].length() == 3 && fstr[2].length() == 4) {
			System.out.println(str + " 是一张社保卡的号码");
		}
		else {
			System.out.println(str + " 不是一张社保卡的号码");
		}
		scanner.close();
	}
}


/*
请输入一个社保卡号码:111-1-111
111-1-111 不是一张社保卡的号码
*/

12.

package day7_27;

import java.util.Scanner;

public class Zuo62 {
	public static void main(String[] args) {
		System.out.print("Enter a string:");
		Scanner scanner = new Scanner(System.in);
		String string = scanner.nextLine();
		String string2 = "";
		for (int i = string.length() - 1; i >= 0; i--) {
			string2 +=string.charAt(i);
		}
		System.out.println("它的反转字符串为:" + string2);
		scanner.close();
	}
}

/*
Enter a string:ABCDE
它的反转字符串为:EDCBA
*/

 13.

package day7_27;

import java.util.Scanner;

public class Zuo63 {
	public static void main(String[] args) {
		System.out.print("Please in a ISBN-12 number:");
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();
		long num = Long.parseLong(str);
		int temp1 = 0, temp2 = 0, result = 0;
        for ( long i = 10000000000L; i >= 1 ; i /= 100){
            temp1 = (int) (num / (i * 10));
            temp2 = (int) (num % (i * 10) / i);
            result += temp1 + 3 * temp2;
            num %= i;
        }
        result = 10 - result % 10;
        if (result == 10)
            result = 0;
        System.out.print("The ISBN-13 number is " + str + result);
		scanner.close();
	}
}

/*
Please in a ISBN-12 number:123456789101
The ISBN-13 number is 1234567891019
*/

 14.

package day7_27;

import java.util.Scanner;

public class Zuo64 {
	public static void main(String[] args) {
		System.out.println("Enter a string:");
		Scanner scanner = new Scanner(System.in);
		String string = scanner.nextLine();
		String string2 = "";
		for (int i = 0; i < string.length(); i++) {
			if (i % 2 != 0) {
				string2 += string.charAt(i);
			}
		}
		System.out.println(string2);
		scanner.close();
	}
}

/*
Enter a string:
ABCDEFGHIJKLM
BDFHJL
*/

 15.

package day7_27;

import java.util.Scanner;

public class Zuo65 {
	public static void main(String[] args) {
		System.out.print("请输入字符串:");
		Scanner scanner = new Scanner(System.in);
		String string = scanner.nextLine();
		int count = 0;
		for (int i = 0; i < string.length(); i++) {
			if (string.charAt(i) >= 'A' && string.charAt(i) <= 'Z') {
				count++;
			}
		}
		System.out.println("其中的大写字母有:" + count + "个");
		scanner.close();
	}
}

/*
请输入字符串:ABCDefgh
其中的大写字母有:4个
*/

 16.

package day7_27;

import java.util.Scanner;

public class Zuo66 {
	public static void main(String[] args) {
		System.out.print("Enter the first string:");
		Scanner scanner = new Scanner(System.in);
		String firstString = scanner.nextLine();
		System.out.print("Enter the second string:");
		Scanner scanner1 = new Scanner(System.in);
		String secondString = scanner1.nextLine();
		String lastString = "";
		for (int i = 0; i < firstString.length();i++) {
			if (firstString.charAt(i) == secondString.charAt(i)) {
				lastString += firstString.charAt(i);
			}
			if (firstString.charAt(i) != secondString.charAt(i)) {
				break;
			}
		}
		System.out.println("The common prefix is:" + lastString);
		scanner.close();
		scanner1.close();
	}
}


/*
Enter the first string:abcde
Enter the second string:abcee
The common prefix is:abc
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值