1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
1 package tset; 2 import java.util.Scanner; 3 public class demo { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 System.out.println("请输入年份:"); 8 int a = input.nextInt(); 9 if (a % 4 == 0 && a % 100 != 0 || a % 400 ==0) { 10 System.out.println("是闰年"); 11 } else { 12 System.out.println("不是闰年"); 13 } 14 // TODO Auto-generated method stub 15 16 } 17 18 }
2.输入一个4位会员卡号,如果百位数字是3的倍数,就输出是幸运会员,否则就输出不是.
1 package tset; 2 import java.util.Scanner; 3 public class demo { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 System.out.println("请输入四位会员卡号:"); 7 int a = input.nextInt(); 8 if ((a % 1000 / 100) % 3 == 0 ) { 9 System.out.println("是幸运会员"); 10 } else { 11 System.out.println("不是幸运会员"); 12 } 13 // TODO Auto-generated method stub 14 15 } 16 17 }
3.已知函数,输入x的值,输出对应的y的值.
x + 3 ( x > 0 )
y = 0 ( x = 0 )
x2 –1 ( x < 0 )
1 package tset; 2 import java.util.Scanner; 3 public class demo { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 System.out.println("请输入x的值(整数):"); 7 int x = input.nextInt(); 8 int y; 9 if(x > 0) { 10 y = x + 3; 11 System.out.println("y的值是:" + y); 12 }else if(x == 0) { 13 System.out.println("y的值是:0"); 14 }else if(x < 0) { 15 y = x * x - 1; 16 System.out.println("y的值是:" + y); 17 } 18 19 } 20 21 }
4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)
1 package tset; 2 import java.util.Scanner; 3 public class demo { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 System.out.println("请输入三角形的三边(整数):"); 7 int a = input.nextInt(); 8 int b = input.nextInt(); 9 int c = input.nextInt(); 10 int d = a + b; 11 int e = a + c; 12 int f = b + c; 13 if(d > c && e > b && f > a) { 14 System.out.println("能构成三角形"); 15 } else { 16 System.out.println("不能构成三角形"); 17 } 18 } 19 }