3.26 第四周作业
1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
1 packageSJWWord; 2 importjava.util.Scanner; 3 publicclass SJWword1 { 4 5 /** 6 * @paramargs 7 */ 8 publicstaticvoid main(String[] args) { 9 // TODO Auto-generated method stub 10 Scanner input=newScanner(System.in); 11 int year=input.nextInt(); 12 if(year%4==0&&year%100!=0||year%400==0) 13 { 14 System.out.println("year是闰年"); 15 16 } 17 else{ 18 System.out.println("year不是闰年"); 19 } 20 } 21
2.输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是.
1 packageSJWWord; 2 importjava.util.Random; 3 importjava.util.Scanner; 4 publicclass SJWwoed2 { 5 6 /** 7 * @paramargs 8 */ 9 publicstaticvoid main(String[] args) { 10 // TODO Auto-generated method stub 11 System.out.println("输入四位会员号:"); 12 Scanner input=newScanner(System.in); 13 Random r=newRandom(); 14 inthy=input.nextInt(); 15 int a=r.nextInt(10); 16 if(hy%1000/100==a){ 17 System.out.println("幸运会员"); 18 } 19 else{ 20 System.out.println("不是幸运会员"); 21 } 22 } 23 24 }
3.已知函数,输入x的值,输出对应的y的值.
1 packageSJWWord; 2 3 importjava.util.Scanner; 4 5 publicclass SJWword3 { 6 7 /** 8 * @paramargs 9 */ 10 publicstaticvoid main(String[] args) { 11 // TODO Auto-generated method stub 12 System.out.println("输入x的值:"); 13 Scanner input=newScanner(System.in); 14 int x=input.nextInt(); 15 int y; 16 if(x>0){ 17 y=x+3; 18 System.out.println("y="+y); 19 } 20 elseif(x==0){ 21 y=x; 22 System.out.println("y="+y); 23 } 24 elseif(x<0){ 25 y=x*2-1; 26 System.out.println("y="+y); 27 } 28 29 } 30 31 }
4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)
1 packageSJWWord; 2 3 importjava.util.Scanner; 4 5 publicclass SJWword4 { 6 7 /** 8 * @paramargs 9 */ 10 publicstaticvoid main(String[] args) { 11 // TODO Auto-generated method stub 12 System.out.println("输入三个数"); 13 Scanner input=newScanner(System.in); 14 int a=input.nextInt(); 15 int b=input.nextInt(); 16 int c=input.nextInt(); 17 if(a+b>c&&a+c>b&&b+c>a) 18 { 19 System.out.println("能构成三角形"); 20 } 21 else{ 22 System.out.println("不能构成三角形"); 23 } 24 } 25 26 }
作业
1 输入年份月份,输出该月的天数(闰年2月29天,条件参考上机练习1)
1 packageSJWWord; 2 importjava.util.Scanner; 3 publicclass SJWword1 { 4 5 /** 6 * @paramargs 7 */ 8 publicstaticvoid main(String[] args) { 9 // TODO Auto-generated method stub 10 Scanner input=newScanner(System.in); 11 System.out.println("输入年份"); 12 int year=input.nextInt(); 13 System.out.println("输入月份"); 14 intyue=input.nextInt(); 15 switch(yue){ 16 case 1: 17 case 3: 18 case 5: 19 case 7: 20 case 8: 21 case 10: 22 case 12: 23 System.out.println("该月的天数是31"); 24 break; 25 case 4: 26 case 6: 27 case 9: 28 case 11: 29 System.out.println("该月的天数是30"); 30 break; 31 case 2: 32 if(year%4==0&&year%100==0||year%400==0) 33 { 34 System.out.println("该月的天数是29"); 35 } 36 else{ 37 System.out.println("该月的天数是28"); 38 } 39 break; 40 default: 41 System.out.println("not"); 42 break; 43 } 44 } 45 46 }
2、给定一个成绩a,使用switch结构求出a的等级
。A:90-100,B:80-89,C:70-79,D:60-69,E:0~59
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 Scanner input=new Scanner(System.in); 6 System.out.println("输入成绩"); 7 int a=input.nextInt(); 8 switch(a/10){ 9 case 10: 10 case 9: 11 System.out.println("A"); 12 break; 13 case 8: 14 System.out.println("B"); 15 break; 16 case 7: 17 System.out.println("C"); 18 break; 19 case 6: 20 System.out.println("D"); 21 break; 22 case 5: 23 case 4: 24 case 3: 25 case 2: 26 case 1: 27 case 0: 28 System.out.println("E"); 29 break; 30 } 31 } 32 33 }
3、输入一个数字,判断是一个奇数还是偶数
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 Scanner input=new Scanner(System.in); 6 System.out.println("输入一个数字"); 7 int a=input.nextInt(); 8 if(a%2==0){ 9 System.out.println(a+"是偶数"); 10 } 11 else { 12 System.out.println(a+"是奇数"); 13 } 14 } 15 }
4、编写程序,判断一个变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 Scanner input=new Scanner(System.in); 6 System.out.println("输入一个数字"); 7 int x=input.nextInt(); 8 if(x==1||x==5||x==10){ 9 System.out.println("x="+x); 10 } 11 else{ 12 System.out.println("x=none"); 13 } 14 } 15 }
5、判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 Scanner input=new Scanner(System.in); 6 System.out.println("输入一个数字"); 7 int x=input.nextInt(); 8 if(x%6==0&&x%5==0){ 9 System.out.println(x+"可以被6整除也可以被5整除");} 10 else if(x%5==0){ 11 System.out.println(x+"只可以被5整除"); 12 } 13 else if(x%6==0){ 14 System.out.println(x+"只可以被6整除"); 15 } 16 else if(x%6!=0&&x%5!=0){ 17 System.out.println(x+"不能被6整除也不能被5整除"); 18 } 19 } 20 }