程序流程控制
顺序结构:程序从上到下逐行执行,中间无任何判断和跳转
分支结构:根据条件,选择性的执行某段代码
if–else和switch–case两种分支语句
循环结构:根据循环条件,重复性的执行某段代码
while
do–while
for三种循环语句
JDK1.5提供foreach循环,方便遍历集合,数组元素
如何从键盘获取不同类型的变量,需要使用scanner类
具体实现步骤:
1 导包:import java.util.Scanner;
2 Scanner的实例化:Scanner scan=new Scanner(System.in);
3 调用Scanner类的相关方法 next()或者nextXxx(),来获取指定类型的变量:int score=scan.nextInt();
import java.util.Scanner;
public class text6{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入岳小鹏期末成绩: (0-100)");
int score = scan.nextInt();
if (score == 100) {
System.out.println("BMW");
} else if (score > 80 && score <= 99) {
System.out.println("XS");
} else if (score >= 60 && score <= 80) {
System.out.println("ipad");
} else {
System.out.println("no");
}
}
}
嵌套
package text1;//num1,num2,num3从小到大输出
import java.util.Scanner;
public class text6{
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int num1=scanner.nextInt();
System.out.println("请输入第一个数字");
int num2=scanner.nextInt();
System.out.println("请输入第二个数字");
int num3=scanner.nextInt();
System.out.println("请输入第三个数字");
if(num1>num2) {
if(num3<num2) {
System.out.println(num3+","+num2+","+num1);
}else if(num3>num1) {
System.out.println(num2+","+num1+","+num3);
}else {
System.out.println(num2+","+num3+","+num1);
}
}else {
if(num3<num1) {
System.out.println(num3+","+num1+","+num2);
}else if(num3>num2){
System.out.println(num1+","+num2+","+num3);
}else {System.out.println(num1+","+num3+","+num2);
}
}
}
}