遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

【参考答案】java基础练习:选择结构(if、switch)

if:if...else...及if嵌套

提示用户输入三个整数,判断并输出最大值、最小值 
package com.qzcsbj;

import java.util.Scanner;

public class Test {
    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;
        if (a > b) {
            max = a;
            min = b;
        } else {
            max = b;
            min = a;
        }
        if (c > max) {
            max = c;
        }
        if (c < min) {
            min = c;
        }
        System.out.println("最大值:" + max);
        System.out.println("最小值:" + min);
    }
}

 

 

如果今天是周日且晴天,就出去嗨

如果今天是周日但不是晴天,睡懒觉

如果今天不是周日,继续996

说明:天气等变量,自己定义即可;主要练习if嵌套

package com.qzcsbj;

/**
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
public class Test {
    public static void main(String[] args) {
        int today=0;
        String weather="晴天";
        if(today==0){
            if(weather.equals("晴天")){
                System.out.println("今天周末,出去嗨");
            }else{
                System.out.println("今天休息,家里睡懒觉");
            }
        }else{
            System.out.println("继续996");
        }
    }
}

 

 

 

由键盘输入三个整数分别存入变量num1、num2、num3,对它们进行排序(使用 if-else if-else),并且从小到大输出。

package com.qzcsbj;

import java.util.Scanner;

/**
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
public class Test {
    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);
            }
        }
    }
}

 

 

 

提示用户输入三角形的边a、b、c,范围必须在 [1,100) 之间,判断三角形的类型:1.非三角形;2.等边三角形;3.直角三角形;4.等腰三角形;5.普通三角形

package com.qzcsbj;

import java.util.Scanner;

public class Test {
    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("普通三角形!");
                }
            }
        }
    }
}

 

 

switch

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

int today=4;
package com.qzcsbj;

public class Test {
    public static void main(String[] args) {
        int today=4;
        switch(today){
            case 1:
                System.out.println("今天跑步");
                break;
            case 2:
            case 3:
            case 4:
                System.out.println("今天打羽毛球");
                break;
            case 5:
                System.out.println("今天游泳");
                break;
            default:
                System.out.println("今天休息");
        }
        System.out.println("switch之后的代码");
    }
}

 

 

输入 2022 年的某个月份和日期,例如 month=4,day=21,经过程序计算,打印出输入的月份和日期是 2022年的第几天:31+28+31+21

package com.qzcsbj;

import java.util.Scanner;

/**
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("month: ");
        int month = scanner.nextInt();

        System.out.print("day: ");
        int day = scanner.nextInt();

        int days = day;

        switch(month - 1){
            case 11:
                days += 30;
            case 10:
                days += 31;
            case 9:
                days += 30;
            case 8:
                days += 31;
            case 7:
                days += 31;
            case 6:
                days += 30;
            case 5:
                days += 31;
            case 4:
                days += 30;
            case 3:
                days += 31;
            case 2:
                days += 28;
            case 1:
                days += 31;
        }

        System.out.println(month + "月" + day + "日是 2022 年的第" + days + "天");
    }
}

 

 

【java百题计划汇总】

详见:https://www.cnblogs.com/uncleyong/p/15828510.html

 

【bak】

原文会持续更新,原文地址:https://www.cnblogs.com/uncleyong/p/17043943.html

 

posted @ 2023-01-12 07:26  全栈测试笔记  阅读(498)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end