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