Java第四次作业
1、输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是。
1 package jjj; 2 import java.util.Random; 3 import java.util.Scanner; 4 public class www1 { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 Scanner input=new Scanner(System.in); 9 System.out.println("请输入您的卡号"); 10 int num=input.nextInt(); 11 int x; 12 x=num/100%10; 13 Random r=new Random(); 14 int a=r.nextInt(10); 15 System.out.println("幸运数字是"+a); 16 if(x==a){ 17 System.out.println("恭喜您成为幸运会员"); 18 } 19 else{ 20 System.out.println("谢谢惠顾"); 21 } 22 23 } 24 25 26 }
2、输入一个年份,判断是不是闰年(能被4整除但不能被100整除,但是能被400整除)
package jjjj; import java.util.Scanner; public class www3 { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner input=new Scanner(System.in); System.out.println("请输入年份"); int x=input.nextInt(); if (x%4==0&&x%100!=0||x%400==0) { System.out.println(x+"是闰年"); } else { System.out.println(x+"不是闰年"); } } }
3、已知函数,输入x的值,输出对应的y值
x+3 (x>0)
y= 0 (x=0)
x*2-1 (x<0)
1 package jjjj; 2 3 import java.util.Scanner; 4 5 public class www2 { 6 7 public static void main(String[] args) { 8 // TODO 自动生成的方法存根 9 Scanner input=new Scanner(System.in); 10 System.out.println("请输入x的值"); 11 int x=input.nextInt(); 12 int y; 13 if (x>0) { 14 y=x+3; 15 System.out.println("y的值为"+y); 16 } else if(x==0){ 17 y=0; 18 System.out.println("y的值为"+y); 19 }else if(x<0){ 20 y=2*x-1; 21 } 22 23 } 24 25 }
4、输入三个数,判断能否构成三角形(任意两边之和大于第三边)
1 package jjjj; 2 import java.util.Scanner; 3 4 public class www4 { 5 6 public static void main(String[] args) { 7 // TODO 自动生成的方法存根 8 Scanner input=new Scanner(System.in); 9 System.out.println("输入三角形的三条边"); 10 System.out.println("输入a"); 11 int a=input.nextInt(); 12 System.out.println("输入b"); 13 int b=input.nextInt(); 14 System.out.println("输入c"); 15 int c=input.nextInt(); 16 if (a+c>b&&b+a>c&&b+c>a) { 17 System.out.println("能构成三角形"); 18 } else{ 19 System.out.println("不能构成三角形"); 20 } 21 } 22 23 24 }
5、输入年份月份,输出该月的天数。
1 package jjjj; 2 import java.util.Scanner; 3 public class www5 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 Scanner input=new Scanner(System.in); 8 System.out.println("输入年份"); 9 int year=input.nextInt(); 10 System.out.println("输入月份"); 11 int month=input.nextInt(); 12 if (year%4==0&&year%100!=0||year%400==0) { 13 if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){ 14 month=31; 15 }else if(month==4||month==6|month==9||month==11){ 16 month=30; 17 }else if(month==2){ 18 month=29; 19 } 20 }else{ 21 if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){ 22 month=31; 23 }else if(month==4||month==6|month==9||month==11){ 24 month=30; 25 }else if(month==2){ 26 month=28; 27 } 28 System.out.println("该年份的月份为"+month); 29 } 30 } 31 32 33 }
6、给定一个成绩a,使用switch结构求出a的等级
A:90-100 B:80-89 C:70-79 D:60-69 E:0-59
1 package jjjj; 2 import java.util.Scanner; 3 public class www6 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 Scanner input=new Scanner(System.in); 8 System.out.println("输入成绩"); 9 int a=input.nextInt(); 10 switch (a/10) { 11 case 6: 12 System.out.println("成绩等级为D"); 13 break; 14 case 7: 15 System.out.println("成绩等级为C"); 16 break; 17 case 8: 18 System.out.println("成绩等级为B"); 19 break; 20 case 9: 21 case 10: 22 System.out.println("成绩等级为A"); 23 break; 24 default: 25 System.out.println("成绩等级为E"); 26 break;} 27 } 28 29 }
7、输入一个数,判断是一个奇数还是偶数
1 package jjjj; 2 import java.util.Scanner; 3 public class www7 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 Scanner input=new Scanner(System.in); 8 System.out.println("输入一个数字"); 9 int a=input.nextInt(); 10 if(a%2==0){ 11 System.out.println("该数为偶数"); 12 }else{ 13 System.out.println("该数为奇数"); 14 } 15 16 } 17 18 19 }
8、编写程序,判断一个变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是10,输出x=10,除了以上几个值,都输出x=none.
package jjjj; import java.util.Scanner; public class www8 { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner input=new Scanner(System.in); System.out.println("请输入一个数值x"); int x=input.nextInt(); if(x==1){ System.out.println("x=1"); }else if(x==5){ System.out.println("x=5"); }else if(x==10){ System.out.println("x=10"); }else{ System.out.println("x=none"); } } }
9、判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除),或只能被6整除(打印能被6整除),不能被5和6整除(打印不能被5和6整除)
1 package jjj; 2 import java.util.Scanner; 3 public class Test9 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 Scanner input=new Scanner(System.in); 11 System.out.println("输入一个数"); 12 int a=input.nextInt(); 13 if(a%5==0&&a%6==0){ 14 System.out.println("能被5和6整除"); 15 } 16 else if(a%5==0&&a%6!=0){ 17 System.out.println("能被5整除"); 18 } 19 else if(a%6==0&&a%5!=0){ 20 System.out.println("能被6整除"); 21 } 22 else if(a%5!=0||a%6!=0){ 23 System.out.println("不能被5或6整除"); 24 } 25 } 26 27 }