1.輸入一個年份,判斷是否是閏年(能被4整除但不能被100整除,或者能被400整除)java
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 )spa
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.輸入三個數,判斷可否構成三角形(任意兩邊之和大於第三邊)3d
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("能構成三角形"); } } }