美文网首页
Java面试题二

Java面试题二

作者: 羋学僧 | 来源:发表于2022-02-28 21:53 被阅读0次

    java基础练习:选择结构(if、switch)

    练习1:提示用户输入三个整数,判断并输出最大值、最小值
    package com.mhg;
    
    import java.util.Scanner;
    
    /**
     * @Time :2022/2/16 20:14
     * @Author :MHG
     **/
    public class TestDay08 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            System.out.print("请输入第一个整数:");
            int a = input.nextInt();
            System.out.print("请输入第二个整数:");
            int b = input.nextInt();
            System.out.print("请输入第三个整数:");
            int c = input.nextInt();
    
            int max,min;
            // 先比较a和b
            if (a > b) {
                max = a ;
                min = b ;
            } else {
                max = b ;
                min = a ;
            }
    
            // 然后将c与max、min进行比较
            if (c > max) {
                max = c ;
            } else {
                min = c ;
            }
    
            System.out.print("最大值:" + max);
            System.out.print("最小值:" + min);
        }
    }
    
    练习2:如果今天是周日且晴天,就出去嗨。如果今天是周日但不是晴天,睡懒觉。如果今天不是周日,继续996。

    说明:天气、星期几的变量,自己定义即可;主要练习if嵌套

    package com.mhg;
    
    import java.util.Scanner;
    
    /**
     * @Time :2022/2/17 20:57
     * @Author :MHG
     **/
    public class TestDay09 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("请输入星期数(整数 1-7):");
            int today = input.nextInt();
    
            if (today == 7) {
    
                System.out.print("请输入天气:");
                String weather = input.next();
    
                if(weather.equals("晴天")) {
                    System.out.print("今天周末,出去嗨!!!");
                }else {
                    System.out.print("今天休息,家里睡懒觉。");
                }
            }else {
                System.out.print("继续996。");
            }
        }
    }
    
    
    练习3:由键盘输入三个整数分别存入变量num1、num2、num3,对它们进行排序(使用 if-else if-else),并且从小到大输出。
    package com.mhg;
    
    import java.util.Scanner;
    
    /**
     * @Time :2022/2/18 15:51
     * @Author :MHG
     **/
    public class TestDay10 {
        public static void main(String[] args) {
            System.out.println("请输入三个整数。");
            Scanner scanner = new Scanner(System.in);
            System.out.print("num1=");
            int num1 = scanner.nextInt();
            System.out.print("num2=");
            int num2 = scanner.nextInt();
            System.out.print("num3=");
            int num3 = scanner.nextInt();
            System.out.println("您输入的三个整数分别为:" + num1 + "," + num2 + "," + num3);
            if (num1 <= num2) {
                if (num3 <= num1) {
                    System.out.println(num3 + "," + num1 + "," + num2);
                } else if (num3 >= num2) {
                    System.out.println(num1 + "," + num2 + "," + num3);
                } else {
                    System.out.println(num1 + "," + num3 + "," + num2);
                }
            } else {
                if (num3 <= num2) {
                    System.out.println(num3 + "," + num2 + "," + num1);
                } else if (num3 >= num1) {
                    System.out.println(num2 + "," + num1 + "," + num3);
                } else {
                    System.out.println(num2 + "," + num3 + "," + num1);
                }
            }
        }
    }
    
    练习4:提示用户输入三角形的边a、b、c,范围必须在 [1,100) 之间,判断三角形的类型:1.非三角形;2.等边三角形;3.直角三角形;4.等腰三角形;5.普通三角形。
    package com.mhg;
    
    import java.util.Scanner;
    
    /**
     * @Time :2022/2/19 14:33
     * @Author :MHG
     **/
    public class TestDay11 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("请输入第一条边: ");
            int a = input.nextInt();
            System.out.print("请输入第二条边: ");
            int b = input.nextInt();
            System.out.print("请输入第三条边: ");
            int c = input.nextInt();
            if (a <1 || a>=100 || b<1 || b>=100 || c<1 || c>=100) {
                System.out.println("数据无效! ");
            }else {
                    if(a+b<=c || a+c<=b || b+c<=a){
                        System.out.println("非三角形! ");
                    } else {
                        if (a==b && a==c){
                            System.out.println("等边三角形! ");
                        }else if(a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a){
                            System.out.println("直角三角形! ");
                        }else if(a==b || a==c || b==c){
                            System.out.println("等腰三角形! ");
                        }else{
                            System.out.println("普通三角形! ");
                        }
                    }
                }
    
            }
    }
    



    练习6:周一跑步,周二、三、四打羽毛球,周五游泳,其它休息
    
    

    相关文章

      网友评论

          本文标题:Java面试题二

          本文链接:https://www.haomeiwen.com/subject/kcklrrtx.html