1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
package Text; import java.util.Scanner; public class zuoye2 { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int year = sc.nextInt(); if(year%4==0 && year%100!=0 || year%400==0) { System.out.println(year+"是闰年"); }else { System.out.println(year+"不是闰年"); } } }
2.输入一个4位会员卡号,如果百位数字是3的倍数,就输出是幸运会员,否则就输出不是.
package Text; import java.util.Scanner; public class zuoye2 { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("请输入4位会员卡号:"); int num= sc.nextInt(); if(num/100%10%3==0) { System.out.println(num+"是幸运会员"); }else { System.out.println(num+"不是幸运会员"); } } }
3.已知函数,输入x的值,输出对应的y的值.
x + 3 ( x > 0 )
y = 0 ( x = 0 )
x2 –1 ( x < 0 )
package Text; import java.util.Scanner; public class zuoye2 { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("请输入x值:"); double x= sc.nextDouble(); if(x>0) { System.out.println("y=x+3="+(x+3)); }else if(x==0){ System.out.println("y="+x); }else { System.out.println("y=x*x-1="+(x*x-1)); } } }
4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)
package Text; import java.util.Scanner; public class zuoye2 { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("请输入第一个数:"); double x= sc.nextDouble(); System.out.println("请输入第二个数:"); double y= sc.nextDouble(); System.out.println("请输入第三个数:"); double z= sc.nextDouble(); double i=x+y; double h=x+z; double l=y+z; if(x>=l || y>=h || z>=i) { System.out.println("不能构成三角形"); }else { System.out.println("能构成三角形"); } } }