3月26日第四周上机练习+作业

上机练习

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

package ddd;
import java.util.Scanner;

public class Sj1 {

    /**
     * @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();
if(year%4==0&&year%100!=0||year%400==0)
{
    System.out.println("闰年!");
    }
else
{
    System.out.println("非闰年!");}
    }

}

 

 

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

 

 

package ddd;

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

public class Sj2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Random a =new Random();
        Scanner input=new Scanner(System.in);
        System.out.println("请输入一个4为卡号");
        int num=input.nextInt();
        int b=a.nextInt();
        if(num/100%10==b){
            System.out.println("该卡号为幸运会员");
        }
        else
        {
            System.out.println("该卡号不是幸运会员");
        }
    }

}

 

 

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

package ddd;

import java.util.Scanner;

public class Sj3 {

    /**
     * @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>0){
    System.out.println("y="+(x+3));
}
else if(x==0){
    System.out.println("y=0");
}
else if(x<0){
    System.out.println("y="+(x*2-1));
}
    }

}

 

 


4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)

package ddd;

import java.util.Scanner;

public class Sj4 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("请输入三个数字:");
int num1=input.nextInt();
int num2=input.nextInt();
int num3=input.nextInt();
if(num1+num2>num3||num1+num3>num2||num2+num3>num1)
{
    System.out.println("这三个数可以构成三角形");
    }
else {
    System.out.println("这三个数不能构成三角形");
}
    }

}

 

 

作业
1 输入年份月份,输出该月的天数(闰年2月29天,条件参考上机练习1)

package ccc;

import java.util.Scanner;

public class Homework1 {

    /**
     * @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();
int month=input.nextInt();
if(year%4==0&&year%100!=0||year%400==0){
    switch(month){
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
 System.out.println(month+"月为31天");
        break;
    case 2:
        System.out.println(month+"月为29天");
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        System.out.println(month+"月为30天");
        break;
        default:
            System.out.println("Error");
            break;
    }
    
    
}
else{
    switch(month){
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
 System.out.println(month+"月为31天");
        break;
    case 2:
        System.out.println(month+"月为28天");
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        System.out.println(month+"月为30天");
        break;
        default:
            System.out.println("Error");
            break;
    }
        
    
}


    }

}

 

 

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

package ccc;

import java.util.Scanner;

public class Homework2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("请输入成绩");
double mark=input.nextDouble();
int level=0;
if(mark>=90&&mark<=100){
    level=1;
}
else if(mark>=80&&mark<=89){
    level=2;
}
else if(mark>=70&&mark<=79){
    level=3;
}
else if(mark>=60&&mark<=59){
    level=4;
}
else if(mark>=0&&mark<=59){
    level=5;
}
switch(level){
case 1:
    System.out.println("A");
break;
case 2:
    System.out.println("B");
break;
case 3:
    System.out.println("C");
break;
case 4:
    System.out.println("D");
break;
case 5:
    System.out.println("E");
break;
default:
    System.out.println("Error");
    break;
}
    }

}

 

 

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

 

package ccc;

import java.util.Scanner;

public class Homework3 {

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

}

 

 

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

package ccc;

import java.util.Scanner;

public class Homework4 {

    /**
     * @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();
        switch(x){
        case 1:
            System.out.println("x=1");
            break;
        case 5:
            System.out.println("x=5");
            break;
        case 10:
            System.out.println("x=10");
            break;
            default:
                System.out.println("x=none");
                break;
        }

    }

}

 

 

5、判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除 ),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)

package ccc;

import java.util.Scanner;

public class Homework5 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("请输入一个数字:");
int num=input.nextInt();
if(num%5==0&&num%6==0){
    System.out.println(num+"能被5和6整除");
}
else if(num%5==0){
    System.out.println(num+"能被5整除");
}
else if(num%6==0){
    System.out.println(num+"能被6整除");
}
else if(num%6!=0&&num%5!=0)
{
    System.out.println(num+"不能被5和6整除");
    }
    }

}

 

posted on 2021-03-27 19:38  Hna  阅读(40)  评论(0编辑  收藏  举报