Java 条件语句 - if…else
一个 if 语句包含一个布尔表达式和一条或多条语句。
if(布尔表达式)
{
//如果布尔表达式为true将执行的语句
}
if 语句后面可以跟 else 语句,当 if 语句的布尔表达式值为 false 时,else 语句块会被执行。
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
if 语句后面可以跟 else if…else 语句,这种语句可以检测到多种可能的情况。
if(布尔表达式 1){
//如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
//如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
//如果布尔表达式 3的值为true执行代码
}else {
//如果以上布尔表达式都不为true执行代码
}
嵌套的 if…else 语句。
if(布尔表达式 1){
//如果布尔表达式 1的值为true执行代码
if(布尔表达式 2){
//如果布尔表达式 2的值为true执行代码
}
}
Java switch case 语句
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break; //可选
//你可以有任意数量的case语句
default : //可选
//语句
}
switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。
几个小案例分享
1、根据年份和月份,判断当月有多少天
import java.util.Scanner;
public class review {
public static void main(String[] args) {
//输入年份和月份,判断该月有多少天
Scanner input = new Scanner(System.in); //创建 Scanner 对象,获取用户的输入
int year = 0, month = 0, days = 0;
System.out.println("请输入年份:");
year = input.nextInt(); //获取年份
System.out.println("请输入月份:");
month = input.nextInt(); //获取月份
//检查输入的月份是否正确
for(;month > 12 || month < 0;)
{
System.out.println("月份输入错误,请重新输入:");
month = input.nextInt();
}
//返回当月的天数
days = rtn_days(year,month);
//控制台输出当月的天数
System.out.println(year+"年"+month+"月有"+days+"天。");
input.close();
}
//判断当月天数
public static int rtn_days(int year, int month)
{
int day = 31;
switch (month){
case 4: case 6: case 9: case 11: //小月
day = 30;
break;
case 2:
day = year%4 == 0 && year%100 != 0 || year%400 == 0 ? 29 :28;//判断闰年
break;
}
return day;
}
}
2、抽奖小游戏
import java.util.Random;
public class practice {
public static void main(String[] args){
Random rand = new Random();
// Random对象的nextInt(n)方法,生成0~n-1的随机整数
//随机生成4个二位数中奖号码,分别对应一~四等奖。
final int FST = 10 + rand.nextInt(90);
final int SND = 10 + rand.nextInt(90);
final int TRD = 10 + rand.nextInt(90);
final int FRT = 10 + rand.nextInt(90);
//随机生成一个抽奖号码
int num = 10 + rand.nextInt(90);
//判断号码是否中奖
String str = "很遗憾,您未中奖!";
if (num == FST){
str = "恭喜您中了一等奖";
}else if(num == SND){
str = "恭喜您中了二等奖";
}else if(num == TRD){
str = "恭喜您中了三等奖";
}else if(num == FRT){
str = "恭喜您中了四等奖";
}
System.out.println(str);
}
}
3、随机生成三个数,并从小到大排序
import java.util.Random;
public class practice {
public static void main(String[] args){
Random rand = new Random();
int a = rand.nextInt(100);
int b = rand.nextInt(100);
int c = rand.nextInt(100);
int t = 0;
//展示生成的三个数
System.out.println("a="+a+"b="+b+"c="+c);
//第一步,a和b比较大小,将较大值传给b,较小值给a
if (a > b){
t = a;
a = b;
b = t;
}
//第二步,b和c比较大小,将较大值传给c,较小值给b,此时c为最大值
if (b > c){
t = b;
b = c;
c = t;
}
//第三步,a和b再次比较大小,将较大值传给b,较小值给a,此时a为最小值
if (a > b){
t = a;
a = b;
b = t;
}
//展示从小到大排好序的三个数
System.out.println("a="+a+"b="+b+"c="+c);
}
}
4、银行账户
import java.util.Scanner;
public class review {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //创建 Scanner 对象,获取用户的输入
final String ACCOUNT = "he", PASSWORD = "123"; //给定一个账号和密码,此处为简化程序,真实场景要复杂的多
double balance = 400.0;//账户余额
System.out.println("请输入账号:");
String acc = input.next();
System.out.println("请输入密码:");
String pwd = input.next();
if(acc.equals(ACCOUNT) && pwd.equals(PASSWORD)){
System.out.println("登录成功。");
System.out.println("1、查询余额 2、存款 3、取款");
System.out.print("请选择你需要办理的业务:");
byte choice = input.nextByte();
//整个switch大括号为一个作用域,不能有同名变量
switch (choice){
case 1:
System.out.println("您的账号余额为:"+balance);
break;
case 2:
System.out.print("请输入您的存款金额:");
double inmon = input.nextDouble();
if(inmon%100 != 0){
System.err.println("存款金额须为100元的整数倍。");
}else{
balance += inmon;
System.out.println("存款成功,存入"+inmon+"元整,当前账户余额:"+balance+"元。");
}
break;
case 3:
System.out.print("请输入您的取款金额:");
double outmon = input.nextFloat();
if(outmon%100 != 0){
System.err.println("取款金额须为100元的整数倍。");
}else{
if(outmon > balance){
System.err.println("余额不足!");
}else{
balance -= outmon;
System.out.println("取款成功,取出"+outmon+"元,当前账户余额:"+balance+"元。");
}
}
break;
default:
System.out.println("没有此项业务,请重新选择。");
break;
}
}else{
System.out.println("登录失败");
}
input.close();
}
}
5、根据年份输出年历
public class MultiplicationTable {
public static void main(String[] args){
calender(2021);
}
public static void calender(int year){
final int WEEK = 7;
int total = 0,day = 0, weekDay;
for (int i = 1900; i<year ; i++){
total += i%4==0&&i%100!=0||i%400==0?366:365;
}
for(int i=1;i<=12;i++){
for(int j=i-1;j<=i;j++){
switch(j){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
day=31;
break;
case 4: case 6: case 9: case 11:
day=30;
break;
case 2:
day = year%4==0 && year%100!=0 || year%400==0 ? 29 : 28;
break;
default:
day=0;
break;
}
if(j<i){
total+=day;
continue;
}
break;
}
weekDay =total%WEEK;
System.out.println("-----------"+i+"月"+"-----------");
System.out.println("日\t一\t二\t三\t四\t五\t六");
if(weekDay != 6){
for(int k=0;k <= weekDay;k++){
System.out.print("\t");
}
}
for(int k =1;k<=day;k++){
System.out.print(k+"\t");
if((total+k)%WEEK==6 && k!=day){
System.out.println();
}
}
System.out.println();
}
}
}
输出结果
-----------1月-----------
日 一 二 三 四 五 六
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
-----------2月-----------
日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28
-----------3月-----------
日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
-----------4月-----------
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
-----------5月-----------
日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
-----------6月-----------
日 一 二 三 四 五 六
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
-----------7月-----------
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
-----------8月-----------
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
-----------9月-----------
日 一 二 三 四 五 六
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
-----------10月-----------
日 一 二 三 四 五 六
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
-----------11月-----------
日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
-----------12月-----------
日 一 二 三 四 五 六
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
6、猜数
import java.util.Random;
import java.util.Scanner;
public class NumberGuess {
public static void main(String[] args) {
//猜数:给一个范围和一个数,在范围内猜这个数,提示大了或者小了,直至猜中。
Random rdm = new Random();
Scanner input = new Scanner(System.in);
final int TARGET = 1+rdm.nextInt(1000);
System.out.println("目标数已生成,请输入你猜测数字(1~1000):");
int num = input.nextInt();
int min = 0, max = 1000;
while(num != TARGET){
if(num >TARGET ){
max = num;
System.out.println("对不起,您猜的数字偏大了,请重猜("+min+"~"+max+"):");
num = input.nextInt();
continue;
}
if(num <TARGET ){
min = num;
System.out.println("对不起,您猜的数字偏小了,请重猜("+min+"~"+max+"):");
num = input.nextInt();
}
}
System.out.println("恭喜你猜对了,数字是:"+TARGET);
input.close();
}
}
运行结果
目标数已生成,请输入你猜测数字(1~1000):
500
对不起,您猜的数字偏小了,请重猜(500~1000):
590
对不起,您猜的数字偏大了,请重猜(500~590):
550
对不起,您猜的数字偏大了,请重猜(500~550):
523
对不起,您猜的数字偏小了,请重猜(523~550):
541
对不起,您猜的数字偏大了,请重猜(523~541):
535
对不起,您猜的数字偏大了,请重猜(523~535):
527
对不起,您猜的数字偏小了,请重猜(527~535):
532
对不起,您猜的数字偏大了,请重猜(527~532):
529
对不起,您猜的数字偏大了,请重猜(527~529):
528
恭喜你猜对了,数字是:528
小结
以上是关于JAVA 条件语句的一些简单的案例分享,欢迎大家交流学习。