2.2. 条件语句¶
2.2.1. if条件语句¶
语法如下¶
if(布尔表达式){
语句序列
}
- 布尔表达式:必要的参数,表示返回的结果必须是一个布尔值,可以是一个单纯的布尔变量或常量,也可以是使用关系或布尔运算符的表达式
- 语句序列:当表达式为true时执行这些语句,若只有一条语句,可以省略“{}”
eg1:
package Number;
public class If_test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int score=95;
if (score >=85){
System.out.println("您真优秀!!");
}
if (score <60){
System.out.println("您需要努力提升自己!!");
}
if ((score >=60) && (score < 85)){
System.out.println("您的成绩还可以,还需要继续努力.....!!");
}
}
}
eg2:
public class Getif { // 创建类
public static void main(String args[]) { // 主方法
int x = 45; // 声明int型变量x,并赋给初值
int y = 12; // 声明int型变量y,并赋给初值
if (x > y) { // 判断x是否大于y
System.out.println("变量x大于变量y"); // 如果条件成立,输出的信息
}
if (x < y) { // 判断x是否小于y
System.out.println("变量x小于变量y"); // 如果条件成立,输出的信息
}
}
}
2.2.2. if…else语句¶
- if … else语句是条件语句中最常用的一种形式,它会针对某种条件有选择地作出处理,通常表现为“如果满足某种条件,就进行某种处理, 否则进行另外一种处理”。
- 语法如下:
if (表达式){
若干语句
}
else {
若干语句
}
eg:
public class Getifelse {
public static void main(String args[]) { // 主方法
int math = 95; // 声明int型局部变量,并赋给初值95
int english = 56; // 声明int型局部变量,并赋给初值56
if (math > 60) { // 使用if语句判断math是否大于60
System.out.println("数学及格了"); // 条件成立时输出信息
} else {
System.out.println("数学没有及格"); // 条件不成立输出的信息
}
if (english > 60) { // 判断英语成绩是否大于60
System.out.println("英语及格了"); // 条件成立输出的信息
} else {
System.out.println("英语没有及格"); // 条件不成立输出的信息
}
}
}
2.2.3. if …else if 多分支语句¶
- if….else if 多分支语句用于针对某一事件的多种情况进行处理,通常表现为:“如果满足某种条件,就进行某种 处理,否则如果满足另一种条件则执行另一种处理” 语法如下:
if(条件表达式1) {
语句序列1
}
else if(条件表达式2){
语句序列2
}
else if(表达式n){
语句序列n
}
1.条件表达式1~条件表达式n:必要参数,可以由多个表达式组成,但最后返回的结果一定要为boolean类型
2.语句序列:可以是一条或多条语句,
当条件表达式1的值为true时,执行语句序列1;
当条件表达式2的值为true时,执行语句序列2;
依此类推,当省略任意一组语句序列时,可以保留其外面的"{}",也可以将"{}"替换为";"。
eg:
public class GetTerm { // 创建主类
public static void main(String args[]) { // 主方法
int x = 20; // 声明int型局部变量
if (x > 30) { // 判断变量x是否大于30
System.out.println("a的值大于30"); // 条件成立的输出信息
} else if (x > 10) { // 判断变量x是否大于10
System.out.println("a的值大于10,但小于30"); // 条件成立的输出信息
} else if (x > 0) { // 判断变量x是否大于0
System.out.println("a的值大于0,但小于10"); // 条件成立的输出信息
} else { // 当以上条件都不成立时,执行的语句块
System.out.println("a的值小于0"); // 输出信息
}
}
}
package if0001;
import java.util.Scanner;
public class If_01 {
public static void main(String[] args) {
System.out.println("请输入您的年龄:");
Scanner scanner = new Scanner(System.in);
int age = scanner.nextInt(); // 接收键盘输入的数据
if ( age >=18) {
System.out.println("成年人");
} else {
System.out.println("您还未成年!");
}
}
}
2.2.4. if-else嵌套语句¶
package if0001;
import java.util.Scanner;
public class IF_qiantao {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入x1:");
int x1 = sc.nextInt(); //获取键盘的输入
System.out.println("请输入x2:");
int x2 = sc.nextInt();
if ( x1 > x2) {
System.out.println("x1 > x2..");
System.out.println("x1 + x2 = " + x1 + x2);
} else {
if (x1 < x2) {
System.out.printf("x1 < x2...x1的值是%d,x2的值是%d \n", x1,x2);
} else {
System.out.printf("x1 = x2 x1的值是%d,x2的值是%d \n",x1, x2);
}
}
}
}
2.2.5. 三目运算符来代替简单的if语句¶
int cost = 1000000;
String type = "Normal";
if (cost > 100000) {
type = "VIP";
} else {
type = "Normal";
}
System.out.println(type);
boolean flag = false;
flag = (cost>100)? true:false;
System.out.println(flag);
2.2.6. switch 多分支语句¶
- 在编程中一个常见的问题就是检测一个变量是否符合某个条件,如果不符合,再用另一个值来检测,依此类推。 if语句实现如下:
if (grade=="A") {
System.out.println("grade的值为A ");
}
if (grade=="B") {
System.out.println("grade的值为B ");
}
- 当需要测试不同的值来给出输出语句时,在Java中,可以用switch语句将动作组织起来,实现“多选一”的选择。 语法如下:
switch(表达式)
{
case 常量值1:
语句块1
[break;]
....
case 常量值n:
语句块n
[break;]
default:
语句块n+1;
[break;]
}
- switch 语句中表达式的值必须是整型,字符型或字符串类型。
- default语句为可选的,如果不存在,且switch语句中表达式的值不与任何case的常量值相同,switch则不做任何处理
eg:
package Number;
public class GetTerm2 {
public static void main(String[] args) {
String str="胡建力学java";
switch (str) {
case "胡":
System.out.println("你姓胡。古 月 的胡");
break;
case "胡建力":
System.out.println("你的名字叫:胡建力");
break;
case "胡建力学java":
System.out.println("哇。胡小健开始学习java了。");
break;
default:
System.out.println("以上条件都不是.....");
}
}
}
eg2:
package Number;
public class GetSwitch {
public static void main(String[] args) {
int week =2;
switch (week) {
case 1:
System.out.println("今天是周1");
break;
case 2:
System.out.println("今天是周2");
break;
case 3:
System.out.println("今天是周3");
break;
default:
System.out.println("sorry I don't know");
}
}
}
eg3
package switch_Packge;
public class switch01 {
public static void main(String[] args) {
int a = 5;
int b = 6;
char op = '*';
switch (op) {
case '+':
System.out.printf("a + b = %d", a + b);
break;
case '-':
System.out.printf("a - b = %d", a - b);
break;
case '*':
System.out.printf("a * b = %d", a * b);
break;
case '/':
System.out.printf("a /b = %d", a / b);
break;
default:
System.out.println("输入的运算符不符合...");
break;
}
}
}
/*
a * b = 30
*/
- 注意: 在switch语句中,case语句后常量表达式的值可以为整数,但绝不可以是实数,例如,下面的代码就是不合法的; case 1.1:
import java.util.Scanner;
public class ch5_16 {
public static void main(String[] args) {
int f;
float k;
Scanner scanner = new Scanner(System.in);
System.out.printf("水果销售\n");
System.out.println("1:(苹果20/斤) 2:(香蕉10/斤) 3:(提子:40/斤)");
System.out.print("请输入您的选择【1/2/3】:");
f = scanner.nextInt(); // 读取选择的水果
System.out.print("请输入采购的斤数:");
k = scanner.nextFloat(); // 读取斤数
switch (f) {
case 1:
System.out.printf("总金额: %f ", (20 * k));
break;
case 2:
System.out.printf("总金额: %f", (10 * k));
break;
case 3:
System.out.printf("总金额: %f", (40 * k));
break;
default:
System.out.println("水果选择错误!");
}
}
}