判断平年闰年
package test1;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if(year%100!=0&&year%4==0||year%400==0) {
System.out.printf("%d是闰年", year);
}else System.out.printf("%d不是闰年",year);
}
}
switch语句判断星期
package test1;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int day = sc.nextInt();
String name;
switch(day){
case 1:
name = "Monday";
break;
case 2:
name = "Tuesday";
break;
case 3:
name = "Wednesday";
break;
case 4:
name = "Thursday";
break;
case 5:
name = "Friday";
break;
case 6:
name = "Saturday";
break;
case 7:
name = "Sunday";
break;
default:
name = "invalid";
}
// System.out.println(name);
//可以用%s格式化输出String
System.out.printf("%s",name);
}
}
遍历
package test1;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
int [] a = {1,2,3,4,5};
for(int x : a) System.out.println(x);
}
}
编辑器优化fori