1. 输入年份月份,输出该月的天数
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入年份:"); int year=input.nextInt(); System.out.println("请输入月份:"); int mouth=input.nextInt(); switch (mouth) { case 4: case 6: case 9: case 11: System.out.println("该月有30天"); case 2: if(year%4!=0||year%100==0&&year%400!=0) { System.out.println("该月有28天"); }else { System.out.println("该月有29天"); } break; default : System.out.println("该月有31天"); break; } } }
2
给定一个成绩a,使用switch结构求出的等级
。A:90-100,B:80-89,C:70-79,D:60-69,E:0~59
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入成绩:"); int a=input.nextInt(); int d; d=a/10; switch (d) { 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; } } }
3、输入一个数字,判断是一个奇数还是偶数
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入一个数:"); int a=input.nextInt(); if(a%2==0) { System.out.println("该数为偶数。"); }else { System.out.println("该数为奇数。"); } } }
4、编写程序, 判断一个变量x的值,如果是1, 输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入一个数:"); 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."); } } }
5、判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除 ),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("输入一个数"); int a=input.nextInt(); if(a%5==0&&a%6==0) { System.out.println("能被5和6整除"); }else if(a%5==0&&a%6!=0){ System.out.println("能被5整除"); }else if(a%6==0&&a%5!=0){ System.out.println("能被6整除"); }else if(a%5!=0||a%6!=0) { System.out.println("不能被5或6整除"); } } }
6。输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入一个年份:"); int year=input.nextInt(); if(year%4==0&&year%100!=0||year%400==0) { System.out.println("该年是闰年。"); }else { System.out.println("该年是平年。"); } } }
7。输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是.
package wang; import java.util.Random; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); Random a=new Random(); int x=a.nextInt(10); System.out.println("请输入一个四位数会员卡号:"); int d=input.nextInt(); if(d%100==x) { System.out.println("该会员是幸运会员ok。"); }else { System.out.println("该会员不是幸运会员不ok。"); } } }
8.已知函数,输入x的值,输出对应的y的值.
x + 3 ( x > 0 )
y = 0 ( x = 0 )
x*2 –1 ( x < 0 )
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入X的值:"); int x=input.nextInt(); if(x>0) { System.out.println("对应y的值为y="+(x+3)); }else if(x==0) { System.out.println("对应y的值为y="+x); }else { System.out.println("对应y的值为y="+(x*2-1)); } } }
9.输入三个数,判断能否构成三角形(任意两边之和大于第三边)
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请分别输入a,b,c的值:"); int a=input.nextInt(); int b=input.nextInt(); int c=input.nextInt(); if(a+b>c&&a+c>b&&b+c>a) { System.out.println("该三边能构成三角形。ok"); }else { System.out.println("该三边不能构成三角形。no ok"); } } }