1 分支结构
1.1 概述
顺序结构的程序虽然能解决计算、输出等问题
但不能做判断再选择。对于要先做判断再选择的问题就要使用分支结构
1.2 形式
1.2.1 分支结构案例练1
/**
* 本类用于练习商品打折案例
*/
public class TestDiscount {
public static void main(String[] args) {
// 需求:接受用户输入的价格,满10000打骨折,满5000打5折,满3000打8折,满1000打9折
System.out.println("请输入商品价格:");
double price = new Scanner(System.in).nextDouble();
double result = price;
if (price >= 10000) {
result = price * 0.1;
}else if (price >= 5000 && price <= 10000) {
result = price * 0.5;
}else if (price >= 3000 && price <= 5000) {
result = price * 0.8;
}else if(price >= 1000 && price <= 3000) {
result = price * 0.9;
}
System.out.println("你所购买的商品原价为:" + price + "," + "应付:" +price + "," + "实付" + result);
}
}
1.2.2 分支结构案例练2
/**
* 本类用于测试学院得分段位
*/
public class TestScore {
public static void main(String[] args) {
System.out.println("请输入你的分数:");
double Score = new Scanner(System.in).nextDouble();
String result = null;
if (Score < 0 || Score > 100) {
System.out.println("请输入0-100之间的数");
}else {
if (Score >= 90 && Score <= 100) {
result = "灰常优秀";
System.out.println("你的分数为:" + Score + "," + "评价为:" + result);
}else if (Score >= 80 && Score <= 89) {
result = "优秀";
System.out.println("你的分数为:" + Score + "," + "评价为:" + result);
}else if (Score >= 70 && Score <= 79) {
result = "良好";
System.out.println("你的分数为:" + Score + "," + "评价为:" + result);
}else if (Score >= 60 && Score <= 69) {
result = "中等";
System.out.println("你的分数为:" + Score + "," + "评价为:" + result);
}else {
result = "你不及格";
System.out.println("你的分数为:" + Score + "," + "评价为:" + result);
}
}
}
}
2 switch
2.1 概述
switch case 语句用来判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
当一个case成立,从这个case向后穿透所有case,包括default,直到程序结束或者遇到break程序才结束
2.2 形式
2.2.1 switch案例练习1
int a = 3;
/**
* 总结:
* 1、变量a支持的类型:byte short char int String
* 2、执行顺序:
* 1、会拿着变量a的值一次与每个case后的值作比较,如果不加break,会向后穿透所有的case
* 包括default
* 3、如果设置了“保底选项default”,并且没有case被匹配到,会执行default里的内容
* 4、break和default是可选的,根据自己的业务需求来决定加不加
*/
switch (a) {
case 1: System.out.println(1);break;
case 2: System.out.println(2);break;
case 3: System.out.println(3);break;
case 4: System.out.println(4);break;
case 5: System.out.println(5);break;
default: System.out.println("没有匹配的数");break;
}
2.2.2 switch案例练习2
String s = "星期二";
switch (s) {
case "星期一": System.out.println("今天干火锅");break;
case "星期二": System.out.println("今天吃烧烤");break;
case "星期三": System.out.println("今天吃烤肉");break;
case "星期四": System.out.println("今天加鸡腿");break;
case "星期五": System.out.println("今天吃炸串");break;
case "星期六": System.out.println("今天吃牛排");break;
case "星期日": System.out.println("今天吃烤全羊");break;
default: System.out.println("没有匹配的选项");break;
}
2.3 switch结构的注意事项
- switch 语句中的变量类型可以是: byte、short、int 、char、String(jdk1.7以后支持)
- switch 语句可以拥有多个 case 语句
- 每个 case 后面跟一个要比较的值和冒号,且此值的数据类型必须与变量的数据类型一致
- 当变量值与 case 语句值相等时,开始执行此case 语句的内容,执行完会判断此行代码是否有break,如果有,结束执行,如果没有,会继续向 后执行穿透所有case,包括default
- switch 语句可以包含一个 default 分支,该分支一般是写在switch 语句的最后
- 如果在default之前的case有break,则default不会执行
3 for循环
3.1 概述
循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。
它由循环体中的条件,判断继续执行某个功能还是退出循环。
根据判断条件,循环结构又可细分为先判断后执行的循环结构和先执行后判断的循环结构。
3.2 形式
3.3 for循环执行顺序
我们明明只写了一句打印语句,为什么打印了多个数字呢?
希望通过下图帮你理解for循环的执行顺序:
3.3.1 for循环案例练习1
public class ForDemo {
public static void main(String[] args) {
/**
* 一个循环究竟执行几次,取决于循环变量可以取到几个值
*/
for (int i = 1; i <= 100 ; i++) {
System.out.println("你真的喜欢我么?" + i);
}
System.out.println("********************************************************");
// 打印数字0-10
for (int i = 0; i <= 10 ; i++) {
System.out.println(i);
}
System.out.println("********************************************************");
// 打印10-1
for (int i = 10; i >= 1; i--) {
System.out.println(i);
}
System.out.println("********************************************************");
for (int i = 8; i <= 8888 ; i = i * 10 + 8) {
System.out.print(i + ",");
}
}
}
3.3.2 for循环案例练习2
public class TestFor2 {
public static void main(String[] args) {
// 需求一:求出1-100以内所有书的和
int sum = 0;
for (int i = 1; i <= 100 ; i++) {
//System.out.println(i);
sum = sum + i;
}
System.out.println("1-100的和为:" + sum);
System.out.println("*******************************************************");
sum = 0;
// 需求二:求出1-100以内偶数的和
for (int i = 1; i <= 100 ; i++) {
//System.out.println(i);
if (i % 2 == 0)
{
sum = sum + i;
}
}
System.out.println("1-100以内的偶数和为:" + sum);
System.out.println("*******************************************************");
// 需求三:求1-100以内奇数的个数
int num = 0;
sum = 0;
for (int i = 1; i <= 100 ; i++) {
if (i % 2 !=0) {
num++;
sum += i;
}
}
System.out.println("奇数的个数有" + num + "个" + "和为:" + sum);
// 普通的四则运算+ - * /
int a = 1;
System.out.println(a + 1);
System.out.println(a);
a = a + 1;
System.out.println(a);
}
}