- JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
- 顺序结构是最简单的算法结构。
- 语句与语句之间,框与框之间是按照从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。
package com.kuang.struct;
public class ShunXuDemo {
public static void main(String[] args) {
//JAVA的顺序结构
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
}
}
选择机构
1、if单选择结构
- 我们很多时候需要去判断一个东西是否可行,然后才去执行,这样一个过程在程序中用if语句来表示。
package com.kuang.struct;
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("");
String s = scanner.nextLine();
//equals:判断字符串是否相等
if (s.equals("Hello")){
System.out.println("s");
}
System.out.println("End");
scanner.close();
}
}
2、if双选择结构
- 那现在有个需求,公司要收购一个软件,成功了,给人支付10万元,失败了,自己找人开发。 这样的需求用一个if就搞不定了,我们需要有两个判断,需要一个双选择结构,所以就有了if- else结构。
- 语法
package com.kuang.struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
//考试大于60分就是及格,小于60分就是不及格。
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生成绩:");
double score = scanner.nextDouble();
if (score >= 60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
}
3、if多选择结构
- 我们发现刚才的代码不符合实际情况,真实的情况还可能存在ABCD,存在区间多级判断。比如 90-100就是A,80-90就是B。等等,在生活中我们很多时候的选择也不仅仅只有两个,所以我 们需要一个多选择结构来处理这类问题!
package com.kuang.struct;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
//if语句至多有1个else语句,else语句在所有的 else if语句之后。
//if语句可以有若干个 else if语句,它们必须在else语句之前。
//一旦其中一个 else if语句检测为trye,其他的 else if以及else语句都将跳过执行。
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生成绩:");
double score = scanner.nextDouble();
if (score == 100) {
System.out.println("满分");
} else if (score < 100 && score >= 90) {
System.out.println("A");
} else if (score < 90 && score >= 80) {
System.out.println("B");
} else if (score < 80 && score >= 70) {
System.out.println("C");
} else if (score < 70 && score >= 60) {
System.out.println("D");
} else if (score < 60 && score >= 0) {
System.out.println("不及格");
}else {
System.out.println("不合法");
}
scanner.close();
}
}
4、嵌套的if结构
- 使用嵌套的if…else语句是合法的。也就是说你可以在另一个if或者 else if语句中使用if或 者 else if语句。你可以像if语句一样嵌套 else if…else。
-
- 思考查找1-100之间的一个数字
3、switch多选择结构
- 多选择结构还有一个实现方式就是swich case语句。
- switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
- switch语句中的变量类型可以是:
- byte、short、int或者char
- 从Java SE 7开始
- switch支持字符串String类型了
- 同时case标签必须为字符串常量或字面量。
package com.kuang.struct;
import java.util.Scanner;
public class SwitchDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String grade = scanner.nextLine();
switch (grade){
case "A":
System.out.println("亿欧秀");
break;
case "B":
System.out.println("good");
break;
case "C":
System.out.println("ok");
break;
case "D":
System.out.println("lll");
break;
default:
System.out.println("aaa");
}
scanner.close();
}
}
反编译,将out文件夹里的.class文件在idea中打开即可反编译
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.kuang.struct;
import java.util.Scanner;
public class SwitchDemo {
public SwitchDemo() {
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
switch (scanner.nextLine()) {
case "A":
System.out.println("亿欧秀");
break;
case "B":
System.out.println("good");
break;
case "C":
System.out.println("ok");
break;
case "D":
System.out.println("lll");
break;
default:
System.out.println("aaa");
}
scanner.close();
}
}



