java基础练习题

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){
        System.out.println("Is "+ userInt + " divisible by 5 and 6? " +
                ( x1 % 5 == 0 && x1 % 6 ==0));
        System.out.println("Is "+ userInt + " divisible by 5 or 6? " +
                (x1 % 5 == 0 || x1 % 6 == 0));
        System.out.println("Is "+ userInt + " divisible by 5 or 6, but not both?  " + (x1 % 5 == 0 ^ x1 % 6 == 0);
    }
}


/*
请输入一个整数:
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 Test;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        System.out.println("请输入一个十进制数");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.println("输入的十进制数为:" + num);
        String hex = "";
        if (num == 0) {
            System.out.println("该数转换为十六进制为: ");
        } else {
            while (num != 0) {
                int hexValue = num % 16;
                char hexNum = ' ';
                if (hexValue <= 9 && hexValue >= 0) {
                    hexNum = (char) (hexValue + '0');
                }
                else {
                    switch (hexValue - 10){
                        case 0:
                            hexNum = 'A';
                            break;
                        case 1:
                            hexNum = 'B';
                            break;
                        case 2:
                            hexNum = 'C';
                            break;
                        case 3:
                            hexNum = 'D';
                            break;
                        case 4:
                            hexNum = 'E';
                            break;
                        case 5:
                            hexNum = 'F';
                            break;
                    }
                }
                hex = hexNum + hex;
                num = num / 16;
            }
            System.out.println("该数转换为十六进制为:" + hex);
        }
    }
}


/*
请输入一个十进制数
30
输入的十进制数为:30
该数转换为十六进制为:1E
*/

8.

package Test;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        System.out.println("请输入你想判断的字符串:");
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        System.out.println(Pan(str));
    }
    public static boolean Pan(String s){
        int t = 0,w = s.length()-1;
        while (t < w){
            if (s.charAt(t) != s.charAt(w)){
                return false;
            }
            t++;
            w--;
        }
        return true;
    }
}


/*
请输入你想判断的字符串:
abcdef
false
*/

9.(找出能被5或6整除,但不能被两者同时整除的数)编写程序,显示从100到200之间所有能被5或6整除,但不能被两者同时整除的数,每行显示10个数。数字之间用一个空格字符隔开。

package Test;

import java.util.Scanner;

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

    public static void Print() {
        int count = 0;
        for (int i = 100; i <= 200; i++) {
            boolean n5 = i % 5 == 0 ? true : false;
            boolean n6 = i % 6 == 0 ? true : false;
            boolean n56 = (n5 == true && n6 ==false) || (n5 == false && n6 ==true);
            if (n56){
                System.out.print(i + " ");
                count ++ ;
                if (count % 10 == 0){
                    System.out.println();
                }
            }
        }
    }

}

/*
100 102 105 108 110 114 115 125 126 130 
132 135 138 140 144 145 155 156 160 162 
165 168 170 174 175 185 186 190 192 195 
198 200 
*/

10.

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 s = sc.nextInt();
        Print(s);
    }

    public static void Print(int n) {
        int count = 0;
        for (int i = 2; i < n ; i++){
            boolean flag = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.print(i + "\t");
                count++;
                if (count % 10 == 0) {
                    System.out.println();
                }
            }
        }
    }

}

/*
请输入你想打印整数之前的素数:
100
2	3	5	7	11	13	17	19	23	29	
31	37	41	43	47	53	59	61	67	71	
73	79	83	89	97	
*/

11.

package Test;

import java.util.Enumeration;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        TongJi();
    }
    public static void TongJi(){
        System.out.println("请输入你想计算的数:");
        Scanner sc = new Scanner(System.in);
        int z = 0;
        int f = 0;
        double sum = 0;
        while (true){
            int s = sc.nextInt();
            if (s == 0){
                break;
            }else if (s > 0){
                z ++;
            }else{
                f ++;
            }
            sum += s;
        }
        System.out.println("正数有:"+z+"个");
        System.out.println("负数有:"+f+"个");
        System.out.println("总和为:" + sum);
        System.out.println("平均数为:" + (sum / (z + f)));

    }
}


/*
请输入你想计算的数:
1 2 3 4 5 -1 0
正数有:5个
负数有:1个
总和为:14.0
总为:6
平均数为:2.3333333333333335
*/

12.(找出一个整数的因子)编写程序,读人一个整数,然后以升序显示它的所有最小因子。例如,若输入的整数是120,那么输出就应该是:2,2,2,3,5。

package Test;

import java.util.Enumeration;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        f();
    }
    public static void f()
    {
        System.out.print("请输入一个整数 : ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        for (int i=2;i<=num;i++)
        {
            while(num % i ==0 )
            {
                num = num / i;
                System.out.print(i + " ");
            }
        }
    }

}


/*
请输入一个整数 : 200
2 2 2 5 5 
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值