【基础题】【分支语句】

1.输入一个字母,如果是大写,输出其小写格式,反之,如果是小写,输出其大写格式。

package com.itheima;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char c =sc.next().charAt(0);
        if(c >= 'A' && c <= 'Z')
            c += 32;
        else if(c >= 'a' && c <= 'z')
            c -= 32;
        System.out.println(c);
    }
}

2.输出两个数中最大的数

package com.itheima;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a,b;
        a = sc.nextInt();
        b = sc.nextInt();
        if(a > b)
            System.out.println(a);
        else if(a < b)
            System.out.println(b);
        else
            System.out.println("两数相等");
    }
}

3.输出三个数中最大的数。

package com.itheima;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a,b,c,max;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        if(a > b){
            if(a > c)
                max = a;
            else
                max = c;
        }
        else{
            if(b > c)
                max = b;
            else
                max = c;
        }
        System.out.println(max);
    }
}

使用条件表达式,更简明、清晰。

package com.itheima;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a,b,c,mid,max;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        mid = a > b?a:b;
        max = mid > c ? mid:c;
        System.out.println(c);
    }
}

4.输入一个年份,判断其是否为闰年。

package com.itheima;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year;
        year  =sc.nextInt();
        if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
            System.out.println(year+"年是闰年");
        else
            System.out.println(year+"年不是闰年");
    }
}

5.分段函数,如果x < 1,y = x; 如果1<= x < 10,y = 2x - 1;如果x >= 10,y = 3x - 1 .

package com.itheima;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double x,y;
        x = sc.nextDouble();
        if(x < 1)
            y = x;
        else if(x < 10)
            y = 2*x - 1;
        else
            y = 3*x - 1;
        System.out.println(y);
    }
}

6.给出一个百分制成绩,要求输出成绩等级’A’,‘B’,‘C’,‘D’,‘E’。90分以上为’A’,80~89分为’B’, 70-79分为’C’, 60-69分为’D’, 60以下为’E’。

package com.itheima;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int score;
        score = sc.nextInt();
        char grade;
        if( score < 60 )
            grade = 'E';
        else if(score < 70)
            grade = 'D';
        else if(score < 80)
            grade = 'C';
        else if(score < 90)
            grade = 'B';
        else
            grade = 'A';
        System.out.println(grade);
    }
}

7.根据给出的年份、月份、输出该月份所含的天数。

package com.itheima;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year, month;
        int days = 0;
        year = sc.nextInt();
        month = sc.nextInt();
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 11:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 12:
                days = 30;
                break;
            case 2:
                if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0 )
                    days = 29;
                else
                    days = 28;
                break;
        }
        System.out.println(year+"年"+month+"月有"+days+"天");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值