JAVA第四周作业0326

package com.homwork4;

import java.util.Scanner;
//输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
public class Exe4_1_1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个年份:");
        int year = input.nextInt();
        if (year%4==0 && year%100!=0||year%400==0) {
            System.out.print(year + "是闰年!");
        } else {
            System.out.print(year + "不是闰年!");
    }
    }

}

 

 

 

package com.homwork4;

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

public class Exe4_1_2 {
    //2.输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是.
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Random r = new Random();
        int luck = r.nextInt(10);
        System.out.println("请输入一个4位会员卡号:");
        int id = input.nextInt();
        System.out.println("恭喜您!");
        if (id%10/10==luck) {
            System.out.println(id + " 您是幸运会员!");
        } else {
            System.out.println(id + " 您是普通会员!");
        }
            System.out.print("luck = "+luck);


    }

}

 

 

 

package com.homwork4;

import java.util.Scanner;

public class Exe4_1_3 {
    /*已知函数,输入x的值,输出对应的y的值.
    x + 3 ( x > 0 )
    y =     0   ( x = 0 )
    x*2 –1 ( x < 0 )*/

    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        System.out.print("请输入X的值:\n X = ");
        double x = input.nextDouble();
        double y ; 
        if (x > 0) {
            y = x+3;
        }else if (x < 0) {
            y = x*2-1;
        } else {
            y = 0;
        }
        System.out.println("y = " + y);


    }

}

 

 

 

package com.homwork4;

import java.util.Scanner;

public class Exe4_1_4 {
    //4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        System.out.println("请输入3个数:");
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        if (a+b>c&&a+c>b&&b+c>a) {
            System.out.println("这3个数能构成三角形!");
        } else {
            System.out.println("这3个数不能构成三角形");
        }


    }

}
package com.homwork4;

import java.util.Scanner;

public class Exe4_2_1 {
//    1  输入年份月份,输出该月的天数(闰年2月29天,条件参考上机练习1)
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个年份:");
        int year = input.nextInt();
        int er = 0;
        if (year%4==0 && year%100!=0||year%400==0) {
            System.out.print(year + "是闰年!");
            er = 1;
        } else {
            System.out.print(year + "不是闰年!");
            er = 0;
    }
        System.out.println("请输入一个月份:");
        int month = input.nextInt();
        switch (month) {
        case 1:case 3:case 5:
        case 7:case 8:case 10:case 12:
            System.out.println(month + "月有31天");
            break;
        case 4:case 6:case 9:case 11:
            System.out.println(month + "月有30天");
            break;
        case 2:
            if (er==1) {
                System.out.println(month + "月有29天");
            } else {
                System.out.println(month + "月有28天");
            }
            break;
        default:
            System.out.println("您输入的月份有误,请输入1~12!");
            break;
}
    }

}

 

 

package text;

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

public class Text {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

//输入年月,输出该月天数
        int year ,math ,day ;
        System.out.print("请输入年份:");
        year = input.nextInt();
        System.out.print("请输入月份:");
        math = input.nextInt();
        for (; math < 1 || math >12;) {
            System.out.print("您输入的月份有误!请重新输入:");
            math = input.nextInt();
        }
        switch(math){
        case 2:
            if(year %4 ==0 && year %100 !=0 || year %400 ==0)    //闰年2月29天
                System.out.println(year +"年"+math +"月有29天");
            else
                System.out.println(year +"年"+math +"月有28天");
            break;
        case 4:case 6:case 9:case 11:
            System.out.println(year +"年"+math +"月有30天");
            break;
        default:
            System.out.println(year +"年"+math +"月有31天");
        }
        
            
        
    }

}
20211026

 

 

 

 

package com.homwork4;

import java.util.Scanner;

public class Exe4_2_2 {
//    给定一个成绩a,使用switch结构求出a的等级。
//    A:90-100,B:80-89,C:70-79,D:60-69,E:0~59
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个成绩:");
        int grade = input.nextInt();
        switch (grade/10) {
        case 9:case 10:
            System.out.println("A等");
            break;
        case 8:
            System.out.println("B等");
            break;
        case 7:
            System.out.println("C等");
            break;
        case 6:
            System.out.println("D等");
            break;
        case 5:case 4:case 3:
        case 2:case 1:case 0:
            System.out.println("E等");
            break;
        default:
            System.out.println("您的输入不正确!");
            break;
        }
    }

}

 

 

package com.homwork4;

import java.util.Scanner;

public class Exe4_2_3 {
//    输入一个数字,判断是一个奇数还是偶数
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个数:");
        int num = input.nextInt();
        if (num==0) {
            System.out.println(num + "既不是奇数,也不是偶数!");
        } else if (num%2==0){
            System.out.println(num + "是偶数!");
        } else {
            System.out.println(num + "是奇数!");
        }

    }

}

 

 

package com.homwork4;

import java.util.Scanner;

public class Exe4_2_4 {
/*    编写程序, 判断一个变量x的值,如果是1, 输出x=1,
        如果是5,输出x=5,如果是 10,输出 x=10,
        除了以上几个值,都输出x=none。    */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入X的值:");
        int x = input.nextInt();
        switch (x) {
        case 1:case 5:case 10:
            System.out.println("X = " + x);
            break;
        default:
            System.out.println("X = none");
            break;
        }
    }

}

 

 

package com.homwork4;

import java.util.Scanner;

public class Exe4_2_5 {
/*    判断一个数字是否能被5和6同时整除(打印能被5和6整除),
    或只能被5整除(打印能被5整除 ),或只能被6整除,(打印能被6整除),
    不能被5或6整除,(打印不能被5或6整除)*/
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个数字:");
        int num = input.nextInt();
        if (num%30==0) {
            System.out.println(num + "能被5和6整除。");
        } else if (num%6==0){
            System.out.println(num + "只能被6整除。");
        } else {
            System.out.println(num + "只能被5整除。");
        }

    }

}

 

posted @ 2021-03-26 18:27  L'童话故事  阅读(50)  评论(0编辑  收藏  举报