Java第四次作业

上机练习

1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)

package Homework;

import java.util.Scanner;

public class Test12 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
           System.out.println("输入一个年份");
        int x=input.nextInt();
        if(x%4==0&&x%100!=0||x%400==0){
            System.out.println("该年份是闰年");
        }else{
            System.out.println("该年份不是闰年");
        }
    }

}

 

2.输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是.

package exercise;

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

public class Test2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Random r = new Random();
        Scanner input = new Scanner(System.in);
        int x = r.nextInt(10);
        System.out.println("幸运数字是" + x);
        System.out.println("输入一个四位数");
        int y = input.nextInt();
        if (x % 1000 / 100 == y) {
            System.out.println("恭喜您是幸运会员");
        } else {
            System.out.println("恭喜您不是幸运会员");
        }
    }
}

 

 

3.已知函数,输入x的值,输出对应的y的值.

        x + 3 ( x > 0 )

y =  0 ( x = 0 )

        x*2 –1 ( x < 0 )

package exercise;

import java.util.Scanner;

public class Test3 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
     Scanner input=new Scanner(System.in);
     System.out.println("输入一个数");
     int x=input.nextInt();
     int y;
     if(x>0){
         y=x+3;
         System.out.println("y="+y);
     }else if (x==0){
             System.out.println("y="+0);
     }else{
         y=x*2-1;
         System.out.println("y="+y);
     }
    }

}

4.

package exercise;

import java.util.Scanner;

public class Test4 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
     Scanner input=new Scanner(System.in);
     System.out.println("输入三个数");
     int x=input.nextInt();
     int y=input.nextInt();
     int z=input.nextInt();
     if(x+y>z&&x+z>y&&y+z>x){
         System.out.println("这三个数能够成三角形");
     }else{
         System.out.println("这三个数不能够成三角形");
     }
    }

}

作业

1.输入年份月份,输出该月的天数(闰年2月29天,条件参考上机联系1)

package Homework;
import java.util.Scanner;
public class Test15 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         Scanner input = new Scanner(System.in);
            System.out.println("输入年份");
            int year = input.nextInt();
            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("该月份31天");
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                System.out.println("该月份30天");
                break;
            case 2:
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    System.out.println("闰年二月29天");
                } else {
                    System.out.println("平年二月28天");
                }
                break;
            default:
                System.out.println("输入的月份无效");
            }

        }

    }

2.给定一个成绩a,使用switch结构求出a的等级
A:90-100,B:80-89,C:70-79,D:60-69,E:0~59

package Homework;

import java.util.Scanner;

public class Test13 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("输入一个成绩grade");
        int grade = input.nextInt();
        switch (grade / 10) {
        case 10:
        case 9:
            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;
        default:
            System.out.println("E");

        }
    }
}

3.输入一个数字,判断是一个奇数还是偶数 

package Homework;

import java.util.Scanner;

public class Test14 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("输入一个数n");
        int n = input.nextInt();
        if (n % 2 == 0) {
            System.out.println(n + "是偶数");
        } else {
            System.out.println(n + "是奇数");
        }
    }

}

4.编写程序, 判断一个变量x的值,如果是1, 输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。

package Homework;

import java.util.Scanner;

public class Test16 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("输入x");
        int x = input.nextInt();
        if (x == 1) {
            System.out.println("x=1");
        } else if (x == 5) {
            System.out.println("x=5");
        } else if (x == 10) {
            System.out.println("x=10");
        } else {
            System.out.println("x=none");
        }
    }

}

 

posted @ 2021-03-26 20:59  曲蔓特  阅读(70)  评论(0编辑  收藏  举报