第四次Java上机作业
1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
import java.util.Scanner; public class Test1 { /** * @param args */ 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("不是闰年"); } } }
2.输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是. import java.util.Random; import java.util.Scanner; public class Test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); Random r=new Random(); System.out.println("输入会员号"); int hao=input.nextInt(); int b=hao/100%10; int a=r.nextInt(10); System.out.println("幸运数是"+a); if (b==a) System.out.println("是幸运会员"); else System.out.println("不是幸运会员"); } }
3.已知函数,输入x的值,输出对应的y的值.
x + 3 ( x > 0 )
y = 0 ( x = 0 )
x*2 –1 ( x < 0 )
import java.util.Scanner; public class Test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int x=input.nextInt(); int y; if (x>0) { y=x+3; System.out.println("y的值是"+y); } if (x==0) System.out.println("y的值是0"); if(x<0){ y=x*x-1; System.out.println("y的值是"+y); } } }
4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)
import java.util.Scanner; public class Test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int x=input.nextInt(); int y=input.nextInt(); int z=input.nextInt(); if ((x>0&y>0&z>0)&x+y>z&x+z>y&z+y>x) System.out.println("能构成三角形"); else System.out.println("不能构成三角形"); } }
1输入年份月份,输出该月的天数
import java.util.Scanner; public class Test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入年份月份"); int year=input.nextInt(); int month=input.nextInt(); if((year%4==0&year%100!=0|year%400==0)&month==2){ System.out.println("天数为29"); }else if((year%4!=0&year%100==0|year%400!=0)&month==2){ System.out.println("天数为28"); } if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){ System.out.println("天数为31"); } if(month==4|month==6|month==9|month==11|month==8){ System.out.println("天数为30"); } } }
2给定一个成绩a,用switch结构求出a的等级
import java.util.Scanner; public class lianxi { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入成绩"); int a=input.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; case 5: case 4: case 3: case 2: case 1: case 0: System.out.println("E"); } } }
3输入一个数字,判断是一个奇数还是偶数
import java.util.Scanner; public class lianxi { /** * @param args */ 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%2==0){ System.out.println("是偶数"); }else{ System.out.println("是奇数"); } } }
4编写程序,判断一个变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是10,输出x=10,除了以上几个值,都输出x=none
import java.util.Scanner; public class LIanxi { /** * @param args */ 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整除)
import java.util.Scanner; public class LIanxi { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输数字"); int n=input.nextInt(); if (n%5==0&n%6==0){ System.out.println("能被5和6整除"); }else if (n%5==0&n%6!=0){ System.out.println("能被5整除"); }else if(n%5!=0&n%6==0){ System.out.println("能被6整除"); }else if(n%5!=0&n%6!=0) { System.out.println("不能被5和6整除"); } } }