程序输入输出
输出语句 Scanner
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.printf("请输入姓名:");
String name=s.nextLine();
System.out.printf("请输入年龄:");
int age=s.nextInt();
System.out.println();
System.out.printf("姓名:%s,年龄:%d岁。%n",name,age);
}
输出语句 System.out.print
public static void main(String[] args) {
System.out.print(2*3);
System.out.print(4*5);
System.out.println();
System.out.println(2*3);
System.out.println(4*5);
System.out.println("success");
}
分支语句
if else
public static void main(String[] args) {
//日历类实例化
var c = Calendar.getInstance();
//获取日历实例对象的星期几1-7
var k = c.get(Calendar.DAY_OF_WEEK);
if (k == 1) System.out.printf("星期日");
if (k == 2) System.out.printf("星期一");
if (k == 3) System.out.printf("星期二");
if (k == 4) System.out.printf("星期三");
if (k == 5) System.out.printf("星期四");
if (k == 6) System.out.printf("星期五");
if (k == 7) System.out.printf("星期六");
int t = 3, a = 4;
if (t == a) {
//()内逻辑表达式值为true 要执行此行代码
System.out.printf("%d=%d", t, a);
} else {
//false
System.out.printf("%d不等于%d%n", t, a);
}
}
public static void main(String[] args) {
//计算平年还是闰年
int y=2024;
if (y%4==0 && y%100 !=0|| y%400==0){
System.out.printf("闰年\n");
}else{
System.out.printf("平年\n");
}
//根据当前时间,输出分时问候
Calendar c= Calendar.getInstance();
int t=c.get(Calendar.HOUR_OF_DAY);
if(t>=5&&t<8){
System.out.printf("早上%n");
}else if (t>=8&&t<12){
System.out.printf("上午%n");
}else if (t>=12&&t<14){
System.out.printf("中午%n");
}else if(t>=12&&t<18){
System.out.printf("下午%n");
} else if (t>=18&&t<23) {
System.out.printf("晚上%n");
} else if (t>=23||t<2) {
System.out.printf("午夜%n");
}else {
System.out.printf("凌晨%n");
}
//三元运算
String s=t<12 ?"上午" :"下午";
System.out.println(s);
}
switch case break default
public static void main(String[] args) {
int k = 3;
switch (k) {
case 1:
System.out.println("星期日");
break;
case 2:
System.out.println("星期一");
break;
case 3:
System.out.println("星期二");
break;
case 4:
System.out.println("星期三");
break;
case 5:
System.out.println("星期四");
break;
case 6:
System.out.println("星期五");
break;
case 7:
System.out.println("星期六");
break;
}
}
//根据月份,输出这个月有多少天
int y = 2024;
int m = 2;
int days = 0;
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
days = y % 400 == 0 || y % 4 == 0 && y % 100 != 0 ? 29 : 28;
}
System.out.printf("%d年%d月有%d天", y, m, days);
}
switch 表达式
注意switch表达式必须有default,如果有多行语句,则在{yield 返回赋值}
public static void main(String[] args) {
//根据月份,输出这个月有多少天
int y = 2023;
int m = 2;
//switch表达式->{yied 常量}
int days = switch (m) {
case 1, 3, 5, 7, 8, 10, 12 -> 31;
case 4, 6, 9, 11 -> 30;
default -> {
int d = 28;
if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0) {
d = 29;
}
yield d;
}
};
System.out.printf("%d年%d月有%d天", y, m, days);
public static void main(String[] args) {
Date d=new Date();
int k= d.getDay();
// switch表达式,有返回值,必须有default ->
String ks=switch (k){
case 1->"星期一";
case 2->"星期二";
case 3->"星期三";
case 4->"星期四";
case 5->"星期五";
case 0->"星期日";
default -> "星期六";
};
System.out.printf("今天是:%s%n",ks);
char kks="日一二三四五六".charAt(k);
System.out.printf("今日是;星期%s%n",kks);
System.out.printf("今日是:%tA%n",d);
}
循环语句
for循环语句
public static void main(String[] args) {
for (int b = 1; b < 6; b++)
System.out.printf("%02d ",b );
System.out.println();
for (int i = 10; i > 5; i--)
System.out.printf("%02d ",i);
System.out.println();
int b=0;
for (int a=1; a<=100;a++) b+=a;
System.out.printf("1+2+3+4+5+...+100=%d",b);
}
public static void main(String[] args) {
//乘法口诀表
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) { // \t是空格的意思
System.out.printf("%d*%d ", j, i, i * j);
}
System.out.println();
}
//反
for (int a = 9; a >= 1; a--) {
for (int t = 1; t <= a; t++) {
System.out.printf("%d*%d ", a, t, t* a);
}
System.out.println();
}
}