1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
package hh; import java.util.Scanner; public class jl { 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%4==0&&a%100!=0||a%400==0){ System.out.println(a+"年是闰年"); }else { System.out.println(a+"年不是闰年是平年"); } } }
2.输入一个4位会员卡号,如果百位数字是3的倍数,就输出是幸运会员,否则就输出不是.
package hh; import java.util.Scanner; public class jl { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入4位会员卡号"); int a=input.nextInt(); if(a/100%10%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 hh; import java.util.Scanner; public class jl { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入x的值"); double x=input.nextDouble(); if(x>0){ double y=x+3; System.out.println(y); }else if(x==0){ System.out.println(0); }else if(x<0){ double y=x*2-1; System.out.println(y); } } }
4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)
package hh; import java.util.Scanner; public class jl { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入a的值"); System.out.println("请输入b的值"); System.out.println("请输入c的值"); double a=input.nextDouble(); double b=input.nextDouble(); double c=input.nextDouble(); if(a+b>c&&a+c>b&&b+c>a&&a-b<c&&a-c<b&&b-c<a){ System.out.println("可以构成三角形"); }else{ System.out.println("不能构成三角形"); } } }
截图: