练习四
1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
package text; public class Jiejue4 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(panduan(2000)); } public static boolean panduan(int n) { if((n%4==0&&n%100!=0)||n%400==0) { return true; } return false; } }
2.输入一个4位会员卡号,如果百位数字是3的倍数,就输出是幸运会员,否则就输出不是. package text; import java.util.Scanner; public class Jiejue4 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("请输入四位会员卡号"); int number= input.nextInt(); int a= number/100%10; for( a=0;a<10;a++) { if(a%3==0) { System.out.println("幸运会员"); } else System.out.println("继续加油"); } } }
3.已知函数,输入x的值,输出对应的y的值. x + 3 ( x > 0 ) y = 0 ( x = 0 ) x2 –1 ( x < 0 ) package apple; import java.util.*; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); int x=input.nextInt(); int y; if(x>0) { y=x+3; } else { if (x<0) { y=(2*x)-1; } else y=0; }
} System.out.println(y);
4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)
package apple; import java.util.Scanner; public class Main { private double a; private double b; private double c; public void Trigon(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please input first side:");//输入第1条边 double a = scan.nextDouble(); System.out.println("Please input second side:");//输入第2条边 double b = scan.nextDouble(); System.out.println("Please input third side:");////输入第3条边 double c = scan.nextDouble(); scan.close(); Main.isTrigon(a, b, c);//判断输入的能否构成三角形以及类型 } public static void isTrigon(double a, double b, double c) {
if(a <= 0 || b <=0 || c <= 0){//如果有小于0的边长,显然不行 System.out.println("Can't"); return; } if (a + b > c && a + c > b && b + c > a) {//2边之和一定要大于第三边 if (a == b || a == c || b == c) {//等腰 if (a == b && b == c) {//等边 System.out.println("可以组成等边三角形"); } else { System.out.println("可以组成等腰三角形"); } return; } System.out.println("可以组成普通三角形");//普通 return; } System.out.println("不能够组成三角形!");//不能构成三角形 } }