if-else语法
- if-else语法,只有一个一个语块被执行
- if和else都是java中的关键字
- 把if-else看作一个表达式,程序整体还是顺序执行的
- 使用if-else来多买两个包子
下面就从买包子问题来理解: 买三个包子, 如果是刚出笼的热包子呢,就多买两个呢?
public class BuyBaoZi {
public static void main(String[] args){
int baozi = 3;
boolean iSHotBaozi = false;
System.out.println("买了"+baozi+"个肉包子");
if (iSHotBaozi){
baozi = baozi + 2;
System.out.println("包子刚出笼,买了"+baozi+"个肉包子");
}else{
System.out.println("买了"+baozi+"个肉包子");
}
}
}
if-else的嵌套
求最大的数
- if-else就是一个语句,可以是另一个语句的一部分,也可以是if-else的一部分,即嵌套。
下从求a,b,c三个数的最大数来理解:
public class JudgeSize {
public static void main(String[] args) {
int a = 99;
int b = 88;
int c = 99;
if (a == b && b == c) {
System.out.println("a,b,c的值相同");
} else {
if (a > b) {
if (a > c) {
System.out.println("a是最大的值,为" + a);
} else {
if (a == c) {
System.out.println("a和c的最大值,为" + a);
} else {
System.out.println("c是最大值,为" + c);
}
}
} else {
if (b > c) {
if (a == b) {
System.out.println("a和b等大,为" + a);
} else {
System.out.println("b最大,为" + b);
}
} else {
if (c == b) {
System.out.println("b和c等大,为" + c);
} else {
System.out.println("c最大,为" + c);
}
}
}
}
}
}
循环for语句
- 使用for语句让程序简洁
- 让程序在满足某条件时,重复执行某个代码块,for是Java中的关键字
- 初始语句在for循环开始前执行一次,以后不再执行;循环体条件表达式在每次循环体执行前会执行,如果为true ,则执行循环体,否则循环结束;循环体后语句会在每次循环执行后被执行;
for (初始语句;循环体条件表达式;循环体后语句) {
for循环
}
下面来看下for语句的简单例程:
public class Print26Chars {
public static void main(String[] args){
char ch = 'A';
int num = ch;
for (int i = 0;i<26;i++){
System.out.println((num+i) + "\t"+(char)(num+i));
}
}
}
Break语句
- break语句可以结束循环
下面来看下在求整除程序中使用break提前结束循环:
public class FindDiv {
public static void main(String[] args) {
int div = 100;
int divsor = 2000000000;
int found = 0;
int n = 10;
String start = "从" + div + "开始";
while (found < n){
if (div < 0){
System.out.println("被除数溢出,计算结束");
break;
}
if (div%divsor == 0){
found++;
System.out.println(div + "可以整除" + divsor + ",商是" + (div / divsor));
}
div++;
}
System.out.println(start+",共找到"+found+"个可以整除"+divsor+"的数。");
System.out.println(div);
}
}
continue语句
- 跳过不符合条件的循环
- continue语句可以结束当次循环的执行,开始下一次循环体的执行
while语句
- 条件表达式结果是一个boolean值,如果为true,则执行循环体,如果为false,则循环结束。
- while循环体是一个代码块,所以while循环也是可以嵌套别的语句,包括while语句,for语句,if-else语句。
while(条件表达式){
while循环体;
}
- do-while语句循环体至少执行一次。
do {
while循环体;
}
while(条件表达式);
switch语句
- switch语句的语法
switch(用于比较的int值){
case 目标值1,对应一个 if else(xxx):
匹配后可以执行的语句
case 目标值2,不可以于别的case子句重复:
匹配后可以执行的语句
default(对应最后的else,可选):
匹配后可以执行的语句
}
- switch语句中用于比较的值,必须是int类型
- switch语句是适用于有固定多个目标匹配值,然后执行不同的逻辑情况
- 必须使用break语句显示的结束一个case子句,否则switch第一个match的case语句开始执行直到遇到break语句或者switch语句结束
- default子句是可选的,如果所有的case语句都没有匹配上,才会执行default中的代码。
- switch语句例程
public class SwitchNum {
public static void main(String[] args) {
int n = 99;
String str = n + "对应的中文数字是:";
switch (n){
case 1:
str+="壹·";
break;
case 2:
str+="贰";
break;
case 3:
str+="叁";
break;
case 4:
str+="肆";
break;
case 5:
str+="伍";
break;
case 6:
str+="陆";
break;
case 7:
str+="柒";
break;
case 8:
str+="扒";
break;
case 9:
str+="玖";
default:
System.out.println("错误的值"+n+"。值需要在大于等于1,小于等于9之间。");
}
System.out.println(str);
}
}
# 相关链接:
[github地址](https://github.com/wangshuningyin/)

网友评论