一、if 语句
1. 基本if-else语句
当条件成立时,执行某些语句;否则执行另一些语句。
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int a = ();
if (a > 5) {
("%d is big!\n", a);
("%d + 1 = %d\n", a, a + 1);
} else {
("%d is small!\n", a);
("%d - 1 = %d\n", a, a - 1);
}
}
}
else 语句可以省略:
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int a = ();
if (a > 5) {
("%d is big!\n", a);
("%d + 1 = %d\n", a, a + 1);
}
}
}
当只有一条语句时,大括号可以省略:
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int a = ();
if (a > 5)
("%d is big!\n", a);
else
("%d is small!\n", a);
}
}
练习:输入一个整数,输出这个数的绝对值。
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int x = ();
if (x > 0)
(x);
else
(-x);
}
}
练习:输入两个整数,输出两个数中较大的那个。
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int a = (), b = ();
if (a > b)
(a);
else
(b);
}
}
if-else语句内部也可以是if-else语句。
练习:输入三个整数,输出三个数中最大的那个。
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int a = (), b = (), c = ();
if (a > b) {
if (a > c)
(a);
else
(c);
} else {
if (b > c)
(b);
else
(c);
}
}
}
2. 常用比较运算符
(1) 大于 >
(2) 小于 <
(3) 大于等于 >=
(4) 小于等于 <=
(5) 等于 ==
(6) 不等于 !=
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int a = (), b = ();
if (a > b) ("%d > %d\n", a, b);
if (a >= b) ("%d >= %d\n", a, b);
if (a < b) ("%d < %d\n", a, b);
if (a <= b) ("%d <= %d\n", a, b);
if (a == b) ("%d == %d\n", a, b);
if (a != b) ("%d != %d\n", a, b);
}
}
3. if-else连写:
输入一个0到100之间的分数,
如果大于等于85,输出A;
如果大于等于70并且小于85,输出B;
如果大于等于60并且小于70,输出C;
如果小于60,输出 D;
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int s = ();
if (s >= 85) {
("A");
} else if (s >= 70) {
("B");
} else if (s >= 60) {
("C");
} else {
("D");
}
}
}
练习:
1.判断闰年。闰年有两种情况:
(1) 能被100整除时,必须能被400整除;
(2) 不能被100整除时,被4整除即可。
输入一个年份,如果是闰年输出yes,否则输出no。
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int year = ();
if (year % 100 == 0) {
if (year % 400 == 0)
("yes");
else
("no");
} else {
if (year % 4 == 0)
("yes");
else
("no");
}
}
}
二、条件表达式
(1) 与 &&
(2) 或 ||
(3) 非 !
例题:输入三个数,输出三个数中的最大值。
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int a = (), b = (), c = ();
if (a >= b && a >= c)
(a);
else if (b >= a && b >= c)
(b);
else
(c);
}
}
练习:用一条if语句,判断闰年。
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int year = ();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
("yes");
else
("no");
}
}
三、switch 语句
注意: swtich语句中如果不加break语句,则从上到下匹配到第一个case后,会顺次执行后面每个case中的语句。
import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int day = ();
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 = "not valid";
}
(name);
}
}import ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner();
int day = ();
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 = "not valid";
}
(name);
}
}