java第四次作业

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

import java.util.*;

public class Homework0326_01{
	public static void main(String[] args){
		System.out.println("请输入年份");
		Scanner s = new Scanner(System.in);
		int year = s.nextInt();
		System.out.println(year % 100 == 0 ? ((year % 400 == 0) ? "是闰年" : "不是闰年") : ((year % 4 == 0) ? "是闰年" : "不是闰年"));
	}
}

  

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

import java.util.*;

public class Homework0326_02{
	public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		System.out.println("请输入你的四位整数会员号");
		int vipId = s.nextInt();
		while(vipId < 1000 || vipId > 9999){
			System.out.println("您输入的号码有误,请重新输入");
			vipId = s.nextInt();
		}
		Random r = new Random();
		int suiJi = r.nextInt(10);
		int vipIdBai = (vipId / 100) % 10;
		if(vipIdBai == suiJi){
			System.out.println(vipId + "是幸运会员");
		}else{
			System.out.println(vipId + "不是幸运会员");
		}
		System.out.println("会员号百位的幸运数字是" + suiJi);
	}
}

  

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

import java.util.*;

public class Homework0326_03{
	public static void main(String[] args){
		System.out.println("请输入x的值");
		Scanner s = new Scanner(System.in);
		double x = s.nextDouble();
		double y = 0;
		if(x > 0){
			y = x + 3.0;
		}else if(x == 0){
			y = 0;
		}else{
			y = x * 2 - 1;
		}
		System.out.println("对应的y值" + y);
	}
}

  

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

import java.util.*;

public class Homework0326_04{
	public static void main(String[] args){
		System.out.println("请输入三边的长度");
		Scanner s = new Scanner(System.in);
		double num1 = s.nextDouble();
		double num2 = s.nextDouble();
		double num3 = s.nextDouble();
		while (num1 < 0 || num2 < 0 || num3 < 0){
			System.out.println("您输入的长度不能为负数,请重新输入");
			num1 = s.nextDouble();
			num2 = s.nextDouble();
			num3 = s.nextDouble();
		}
		if((num1 + num2) > num3 && (num1 + num3) > num2 && (num2 + num3) > num1){
			System.out.println("是三角形");
		}else{
			System.out.println("不是三角形");
		}
	}
}

  

5.输入年份月份,输出该月的天数(闰年2月29天)

import java.util.*;

public class Homework0326_05{
	public static void main(String[] args){
		boolean runNian = RunNian.panDuan();
		System.out.println(runNian == true ? "是闰年" : "不是闰年");
		System.out.println("请输入月份");
		Scanner s = new Scanner(System.in);
		int month = s.nextInt();
		while(month < 1 || month > 12){
			System.out.println("您输入的月份有误,请重新输入月份");
			month = s.nextInt();
		}
		int day = (runNian && month == 2) ? 29 : 28;
		if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
			day = 31;
		}else if(month == 4 || month == 6 || month == 9 || month == 11){
			day = 30;
		}
		System.out.println("该月有" + day + "天");
	}
}
class RunNian{
	public static boolean panDuan(){
		System.out.println("请输入年份");
		Scanner s = new Scanner(System.in);
		int year = s.nextInt();
		boolean runNian = (year % 100 == 0 ? ((year % 400 == 0) ? true : false) : ((year % 4 == 0) ? true : false));
		return runNian;
	}
}

  

6.给定一个成绩a,使用switch结构求出a的等级

A: 90 - 100 B: 80 - 89 C: 70 - 79 D: 60 - 69 E: 0 - 59

import java.util.*;

public class Homework0326_06{
	public static void main(String[] args){
		System.out.println("请输入您的成绩");
		Scanner s = new Scanner(System.in);
		int achievement = s.nextInt();
		while(achievement < 0 || achievement > 100){
			System.out.println("您输入的成绩有误,请重新输入");
			achievement = s.nextInt();
		}
		switch(achievement / 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");
				break;
			}
		}
	}
}

  

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

import java.util.*;

public class Homework0326_07{
	public static void main(String[] args){
		System.out.println("请输入一个整数");
		Scanner s = new Scanner(System.in);
		int num = s.nextInt();
		if(num > 0){
			System.out.println(num % 2 == 0 ? num + "是偶数" : num + "是奇数");
		}else{
			System.out.println("小于1的整数没有奇偶之分");
		}
	}
}

  

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

import java.util.*;

public class Homework0326_08{
	public static void main(String[] args){
		System.out.println("请输入一个整数");
		Scanner s = new Scanner(System.in);
		int x = s.nextInt();
		switch(x){
			case 1:{
				System.out.println("x = " + x);
				break;
			}
			case 5:{
				System.out.println("x = " + x);
				break;
			}
			case 10:{
				System.out.println("x = " + x);
				break;
			}
			default:{
				System.out.println("x = none");
				break;
			}
		}
	}
}

  

9.判断一个数字是否能同时被5和6同时整除(打印能同时被5和6整除)或者只能被5整除(打印能被5整除)

或者只能被6整除(打印能被6整除)不能被5或6整除(打印不能被5或6整除)

 

import java.util.*;

public class Homework0326_09{
	public static void main(String[] args){
		System.out.println("请输入一个整数");
		Scanner s = new Scanner(System.in);
		int num = s.nextInt();
		if(num % 5 == 0 && num % 6 == 0){
			System.out.println("能同时被5和6整除");
		}else if(num % 5 == 0|| num % 6 == 0){
			System.out.println(num % 5 == 0 ? "能被5整除" : "能被6整除");
		}else{
			System.out.println("不能被5或6整除");
		}
	}
}

  

————————————————————

————————————————————

2021年3月26日作业完成

 
posted @ 2021-03-26 20:33  音无彩名  阅读(86)  评论(0编辑  收藏  举报