java第四次作业
1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
package School.Day4; import java.util.Scanner; public class Test01 { public static void main(String[] args){ System.out.print("输入年份: "); Scanner s = new Scanner(System.in); int year = s.nextInt(); if(year%100==0){ if(year%400 ==0){ System.out.println("该年是闰年"); }else{ System.out.println("该年是平年"); } }else{ if(year%4==0||year%400==0){ System.out.println("该年是闰年"); }else{ System.out.println("该年是平年"); } } } }
2.输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是.
package School.Day4; import java.util.Random; import java.util.Scanner; public class Test02 { public static void main(String[] args){ Random r = new Random(); Scanner s = new Scanner(System.in); System.out.print("输入会员号: "); int huiyuan = s.nextInt(); int x = r.nextInt(9); System.out.println("幸运随机数: " + x); if(huiyuan%1000/100==x){ System.out.println("恭喜您,中奖了!"); }else{ System.out.println("很遗憾,没中奖!"); } } }
3.已知函数,输入x的值,输出对应的y的值.
x + 3 ( x > 0 )
y = 0 ( x = 0 )
x*2 –1 ( x < 0 )
package School.Day4; import java.util.Scanner; public class Test03 { public static void main(String[] args){ System.out.print("输入X: "); Scanner s = new Scanner(System.in); int x = s.nextInt(); int y; if(x>0){ y = x+3; }else if(x==0){ y = 0; }else { y = 2*x-1; } System.out.println("Y==" + y); } }
4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)
package School.Day4; import java.util.Scanner; public class Test04 { public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.print("输入边长: "); double a = s.nextDouble(); System.out.print("输入边长: "); double b = s.nextDouble(); System.out.print("输入边长: "); double c = s.nextDouble(); if(a+b>c&&a+c>b&&b+c>a){ System.out.println("可以构成三角形"); }else{ System.out.println("不可以构成三角形"); } } }
5. 输入年份月份,输出该月的天数
package School.Day4; import java.util.Scanner; public class Test05 { public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.print("输入年份: "); int year = s.nextInt(); System.out.print("输入月份: "); int month = s.nextInt(); int day; switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 2: day = 28; break; default: day = 30; } if(year%100==0){ if(year%400==0&&month ==2){ day = 29; } }else{ if(year%4==0||year%400==0){ if (month == 2) { day = 29; } } } System.out.println( year + "年" + month + "月有" + day + "天" ); } }
6.给定一个成绩a,使用switch结构求出a的等级
A:90-100,B:80-89,C:70-79,D:60-69,E:0~59
package School.Day4; import java.util.Scanner; public class Test06 { public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.print("输入成绩: "); int a = s.nextInt(); switch(a/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"); } } }
7.输入一个数字,判断是一个奇数还是偶数
package School.Day4; import java.util.Scanner; public class Test07 { public static void main(String[] args){ System.out.print("输入数字: "); Scanner s = new Scanner(System.in); int x = s.nextInt(); if(x%2==0){ System.out.println("这个数是偶数"); }else{ System.out.println("这个数是奇数"); } } }
8.编写程序, 判断一个变量x的值,如果是1, 输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。
package School.Day4; import java.util.Scanner; public class Test08 { public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.print("输入变量: "); int x = s.nextInt(); switch(x){ case 1: x = 1; break; case 5: x = 5; break; case 10: x = 10; break; default: System.out.println("none"); return; } System.out.println("变量是: " + x); } }
9.判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除 ),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)
package School.Day4; import java.util.Scanner; public class Test09 { public static void main(String[] args) { System.out.print("输入一个数: "); Scanner s = new Scanner(System.in); int x = s.nextInt(); if (x%5==0 && x%6==0) { System.out.println(x + "可以被5和6整除"); }else if (x%5==0) { System.out.println(x + "可以被5整除"); }else if (x%6==0) { System.out.println(x + "可以被6整除"); }else{ System.out.println(x + "不可以被5或者6整除"); } } }