java第四次作业
上机练习
1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
1 package pratice_oc; 2 3 import java.util.Scanner; 4 5 public class pra_001 { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner input=new Scanner(System.in); 10 System.out.println("请输入一个四位年份"); 11 int year=input.nextInt(); 12 if(year%4==0&&year%100!=0||year%400==0) 13 { 14 System.out.println(year+"是闰年"); 15 } 16 else 17 { 18 System.out.println(year+"不是闰年"); 19 } 20 } 21 22 }
2.输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是.
1 package pratice_oc; 2 3 import java.util.Random; 4 import java.util.Scanner; 5 6 public class pra_002 { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 Scanner input=new Scanner(System.in); 11 Random f=new Random(); 12 System.out.println("请输入四位会员卡号"); 13 int a=input.nextInt(); 14 int b=f.nextInt(10); 15 int c; 16 if(a>=1000&&a<10000) 17 { 18 c=a%1000/100; 19 System.out.println("本次幸运数字是"+b); 20 if(b==c) 21 { 22 System.out.println("恭喜!您已成为幸运会员"); 23 } 24 else 25 { 26 System.out.println("很遗憾!您未能成为幸运会员"); 27 } 28 } 29 else 30 { 31 System.out.println("卡号格式不正确"); 32 } 33 //System.out.println(b); 34 } 35 36 }
3.已知函数,输入x的值,输出对应的y的值.
1 package pratice_oc; 2 3 import java.util.Scanner; 4 5 public class pra_003 { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner input=new Scanner(System.in); 10 System.out.println("请输入X的值"); 11 int x=input.nextInt(); 12 if(x>0) 13 { 14 System.out.println("Y的值为"+( x + 3 )); 15 } 16 else if(x==0) 17 { 18 System.out.println("Y的值为0"); 19 } 20 else 21 { 22 System.out.println("Y的值为"+( x*2-1 )); 23 } 24 } 25 26 }
4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)
1 package pratice_oc; 2 3 import java.util.Scanner; 4 5 public class pra_004 { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner input=new Scanner(System.in); 10 System.out.println("请分别输入三个数字"); 11 int x=input.nextInt(); 12 int c=input.nextInt(); 13 int v=input.nextInt(); 14 if(x+c>v&&x+v>c&&c+v>x) 15 { 16 System.out.println("这三个数字可以构成三角形"); 17 } 18 else 19 { 20 System.out.println("这三个数字不可以构成三角形"); 21 } 22 } 23 24 }
作业
1 输入年份月份,输出该月的天数(闰年2月29天,条件参考上机练习1)
1 package smz_pro_1; 2 3 import java.util.Scanner; 4 5 public class pratice_1 { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 10 Scanner input=new Scanner(System.in); 11 12 System.err.println("请分别输入年份、月份"); 13 int year=input.nextInt(); 14 int month=input.nextInt(); 15 16 if(year%4==0&&year%100!=0||year%400==0) 17 { 18 System.out.println(year+"是闰年"); 19 switch(month) 20 { 21 case 1:System.out.println("该月份有31天");break; 22 case 2:System.out.println("该月份有29天");break; 23 case 3:System.out.println("该月份有31天");break; 24 case 4:System.out.println("该月份有30天");break; 25 case 5:System.out.println("该月份有31天");break; 26 case 6:System.out.println("该月份有30天");break; 27 case 7:System.out.println("该月份有31天");break; 28 case 8:System.out.println("该月份有31天");break; 29 case 9:System.out.println("该月份有30天");break; 30 case 10:System.out.println("该月份有31天");break; 31 case 11:System.out.println("该月份有30天");break; 32 case 12:System.out.println("该月份有31天");break; 33 default:System.out.println("月份格式不合法"); 34 } 35 } 36 else 37 38 { 39 System.out.println(year+"不是闰年"); 40 switch(month) 41 { 42 case 1:System.out.println("该月份有31天");break; 43 case 2:System.out.println("该月份有28天");break; 44 case 3:System.out.println("该月份有31天");break; 45 case 4:System.out.println("该月份有30天");break; 46 case 5:System.out.println("该月份有31天");break; 47 case 6:System.out.println("该月份有30天");break; 48 case 7:System.out.println("该月份有31天");break; 49 case 8:System.out.println("该月份有31天");break; 50 case 9:System.out.println("该月份有30天");break; 51 case 10:System.out.println("该月份有31天");break; 52 case 11:System.out.println("该月份有30天");break; 53 case 12:System.out.println("该月份有31天");break; 54 default:System.out.println("月份格式不合法"); 55 } 56 } 57 58 } 59 }
2、给定一个成绩a,使用switch结构求出a的等级
1 package smz_pro_1; 2 3 import java.util.Scanner; 4 5 public class pra_2 { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner iput=new Scanner(System.in); 10 System.out.println("请输入成绩"); 11 int sco=iput.nextInt(); 12 int a=sco/10; 13 switch(a) 14 { 15 case 1: 16 case 2: 17 case 3: 18 case 4: 19 case 5:System.out.println("成绩为E");break; 20 case 6:System.out.println("成绩为D");break; 21 case 7:System.out.println("成绩为C");break; 22 case 8:System.out.println("成绩为B");break; 23 case 9: 24 case 10:System.out.println("成绩为A");break; 25 default:System.out.println("输入的成绩有误"); 26 } 27 } 28 29 }
3、输入一个数字,判断是一个奇数还是偶数
1 package smz_pro_1; 2 3 import java.util.Scanner; 4 5 public class smz { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner input=new Scanner(System.in); 10 System.out.println("请输入一个整数"); 11 int a=input.nextInt(); 12 if(a%2==0&&a!=0) 13 { 14 System.out.println("该数为偶数"); 15 } 16 else if(a%2!=0&&a!=0) 17 { 18 System.out.println("该数为奇数"); 19 20 } 21 else 22 { 23 System.out.println("0既不是奇数也不是偶数"); 24 } 25 } 26 27 }
4、编写程序, 判断一个变量x的值,如果是1, 输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。
1 package smz_pro_1; 2 3 import java.util.Scanner; 4 5 public class smz_1 { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner input=new Scanner(System.in); 10 System.err.println("请输入一个数X"); 11 int a=input.nextInt(); 12 switch(a) 13 { 14 case 1:System.out.println("X=1");break; 15 case 5:System.out.println("X=5");break; 16 case 10:System.out.println("X=10");break; 17 default:System.out.println("non"); 18 } 19 } 20 21 }
5、判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除 ),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)
1 package smz_pro_1; 2 3 import java.util.Scanner; 4 5 public class smz_2 { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner input=new Scanner(System.in); 10 11 System.err.println("请输入一个整数"); 12 int a=input.nextInt(); 13 if(a%5==0&&a%6==0) 14 { 15 System.err.println(a+"能被5和6整除"); 16 } 17 else if(a%5==0&&a%6!=0) 18 { 19 System.err.println(a+"能被5整除"); 20 } 21 else if(a%5!=0&&a%6==0) 22 { 23 System.err.println(a+"能被6整除"); 24 } 25 else 26 { 27 System.err.println(a+"不能被5和6整除"); 28 } 29 } 30 31 }