第4周上机练习+作业

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

package com.homework;

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 year=input.nextInt();
if(year%4==0&&year%100!=0||year%400==0)
{
	System.out.println("YES");
}
else
{
	System.out.println("no");
}
	}

}

  

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

package com.homework;

import java.util.Random;
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);
		Random r=new Random();
		int c=r.nextInt(10);
		 System.out.println("输入会员号");
		 int a=input.nextInt();
		 int b=a/100%10;
		System.out.println(c);

		 if(b==c)
		 {
		System.out.println("yes");
			}
		 else
		 {
			 System.out.println("NO");
		 }

	}

}

  

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

package KTLX;

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

}

  

 

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

package com.homework;

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("num1");
		int a=input.nextInt();
		System.out.println("num2");
		int b=input.nextInt();
		System.out.println("num3");
		int c=input.nextInt();
		if(a+b>c&&a+c>b&&b+c>a)
		{
			System.out.println("YES");
		}
		else
		{
			System.out.println("NO");
		}


	}

}

  

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

package com.homework;

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();
		System.out.println("请输入月份");
		int month = input.nextInt();
		int day;
		if (((year%4==0&&year%100!=0)||year%400==0)&&month==2) 
		{
            day=29;
            System.out.println("该月的天数为"+day);
        } else if(month==1) {
            day=31;
            System.out.println("该月的天数为"+day);
        }else if(month==2) {
            day=28;
            System.out.println("该月的天数为"+day);
        }else if(month==3) {
            day=31;
            System.out.println("该月的天数为"+day);
        }else if(month==4) {
            day=30;
            System.out.println("该月的天数为"+day);
        }else if(month==5) {
            day=31;
            System.out.println("该月的天数为"+day);
        }else if(month==6) {
            day=30;
            System.out.println("该月的天数为"+day);
        }else if(month==7) {
            day=31;
            System.out.println("该月的天数为"+day);
        }else if(month==8) {
            day=31;
            System.out.println("该月的天数为"+day);
        }else if(month==9) {
            day=30;
            System.out.println("该月的天数为"+day);
        }else if(month==10) {
            day=31;
            System.out.println("该月的天数为"+day);
        }else if(month==11) {
            day=30;
            System.out.println("该月的天数为"+day);
        }else if(month==12) {
            day=31;
            System.out.println("该月的天数为"+day);
        }
	}

}

  


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

package com.homework;

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("请输入成绩");
int grade=input.nextInt();
switch(grade/10)
{
case 10:
    System.out.println("等级为A");
    break;
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");
    break;
}
	}

}

  


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

package com.homework;

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("输入一个数");
		int x=input.nextInt();
		if(x%2==0)
		{
			System.out.println("X是偶数");
		}
		else
		{
			System.out.println("X=奇数");
		}


	}

}

  


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

package com.homework;

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("输入一个数");
		int x=input.nextInt();
		if(x==1)
		{
			System.out.println("X="+x);
		}
		else if(x==5)
		{
			System.out.println("X="+x);
		}
		else if(x==10)
		{
			System.out.println("X="+x);
		}
		else
		{
			System.out.println("X=none");
		}


	}

}

  


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

package com.homework;

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("能被5和6整除");
}
else if(num%5==0)
{
	System.out.println("能被5整除");
}
else if(num%6==0)
{
	System.out.println("能被6整除");
}
else
{
	System.out.println("不能被5或者6整除");
}
	}

}

  

 

posted @ 2021-03-27 22:14  sr0614  阅读(37)  评论(0编辑  收藏  举报