目录
流程控制
顺序结构
指程序从上到下从左到右依次运行
分支结构——判断结构
if语句
if(逻辑值){
代码块;
}
- 执行顺序:当逻辑值为true的时候,执行代码块;反之不执行
例子:判断奇偶数
import java.util.Scanner;
public class IfDemo {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int i = s.nextInt();
if(i % 2 == 0){
System.out.println(i + " is an even number .");
} else {
System.out.println(i + " is an odd number .");
}
}
}
当if结构的代码块只有一句的时候,{}可以省略不写,但是最好写上
if...else语句
if(逻辑值){
Code 1;
} else {
Code 2;
}
- 执行顺序:当逻辑值为true的时候,执行代码块1; 反之执行代码块2
例子:对学生考试成绩进行等级划分
import java.util.Scanner;
public class IfElseIfDemo {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
double score = s.nextDouble();
char level = 'E';
if(score > 100 || score < 0){
System.out.println("Error");
} else if(score >= 90){
level = 'A';
} else if(score >= 80){
level = 'B';
} else if(score >= 70){
level = 'C';
} else if(score >= 60){
level = 'D';
} else {
level = 'E';
}
System.out.println(level);
}
}
if...else if...else语句
if(逻辑值1){
Code1;
} else if(逻辑值2){
Code2.;
} else if(逻辑值3){
Code3;
} else {
Code4;
}
- 执行顺序:先执行逻辑值1,如果逻辑值1为true,执行Code1;反之执行逻辑值2,如果逻辑值2为true,执行Code2,以此类推
例子:输入月份,输出季节。3-5春,6-8夏,9-11秋,12,1,2冬
import java.util.Scanner;
public class IfElseIfExer {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int month = s.nextInt();
String season = "";
if(month > 12 || month < 1){
season = "Wrong month !";
} else if(month >= 3 && month <= 5){
season = "Spring";
} else if(month >= 6 && month <= 8){
season = "Summer";
} else if(month >= 9 && month <= 11){
season = "Autumn";
} else {
season = "Winter";
}
System.out.println(season);
}
}
Scanner:从控制台输入数据
import java.util.Scanner; //导包语句
Scanner s = new Scanner(System.in); //输入流
int i = s.nextInt();
double d = s.nextDouble();
String s = s.nextLine();
char c = (char)s.nextInt();
分支结构——选择结构
switch(表达式){
case 情况1:Code1;break;
case 情况2:Code2;break;
……
default:Code ; break;
}
说明:
- 表达式的运算结果只能是byte/short/int/char
- 从JDK1.7开始,允许表达式的结果是String;从JDK1.5开始,允许表达式的结果是枚举常量
- break关键字表示当前选项的结束,在switch语句中,只有遇到了break语句或者执行switch语句的末尾才会结束。有break的时候,选项之间的顺序没有影响;只要有一个或者一个以上的选项没有break,那么这时候顺序会有影响。break表示当前一层选择语句的结束
import java.util.Scanner;
public class SwitchDemo {
public static void main(String[] args){
int i = 5;
Scanner s = new Scanner(System.in);
int j = s.nextInt();
switch(j){
case 1: i += 3;break;
case 2: i += 5;break;
case 3: i += 7;break;
default: i += 9;break;
}
System.out.println(i);
}
}
注:判断的是一个范围的时候,建议使用if-else-if结构;如果情况相对比较固定的时候,建议使用switch-case语句
循环结构
while语句
while(逻辑值){
Code;
}
- 执行顺序:先判断逻辑值。如果逻辑值为true,执行Code,然后再次判断逻辑值,如果为true,继续循环;如果为false,循环结束
注:
- 循环的三要素:定义循环变量;循环条件的控制;循环变量的改变
- while语句中,如果代码块只有一句,{}也可以省略
例子:
1.求1-100的和
public class WhileDemo {
public static void main(String[] args){
int i = 1;
int sum = 0;
while(i <= 100){
sum += i;
i++;
}
System.out.println(sum);
}
}
2.打印300以内7的倍数
public class WhileExer {
public static void main(String[] args){
int i = 1;
while(i <= 300 ){
if(i % 7 == 0){
System.out.println(i);
}
i++;
}
}
}
3. 输入一个数,输出是几位数
import java.util.Scanner;
public class WhileExer2 {
public static void main(String[] ars){
Scanner s = new Scanner(System.in);
int number = s.nextInt();
int i = 0;
while(number != 0){
number /= 10;
i++;
}
System.out.println(i);
}
}
4. 将一个数字转化成二进制形式显示
import java.util.Scanner;
public class WhileExer3 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
/*
while(number < 0){
number = scan.nextInt();
}
*/
for(;number < 0;){
number = scan.nextInt();
}
String s = "";
while(number != 0){
s = number % 2 + s;
number /= 2;
}
System.out.println(s);
}
}
do...while语句
do{
Code;
} while(逻辑值);
- 执行顺序:先执行Code,执行逻辑值,如果逻辑值为true,再次执行Code,如果逻辑值为false,结束循环
注:Code至少执行一次
for语句
for(循环变量的定义; 循环条件的判断;循环变量的改变){
Code ;
}
注:
- 当循环变量的改变相对比较规律的时候,建议使用for循环
- 当不知道循环次数的时候,建议使用while循环。
- while循环,循环变量定义在循环外,循环结束之后循环变量依然可以使用;for循环的循环变量是定义在for循环里边的,循环结束之后,循环变量不能继续使用
例子:
1. 打印300以内7的倍数
public class ForExer {
public static void main(String[] args){
for(int i = 1; i <= 300; i++){
if(i % 7 == 0){
System.out.println(i);
}
}
}
}
2. 判断一个数是否是一个质数(一个数除了1和本身以外没有别的因数)
import java.util.Scanner;
public class ForExer2 {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int number = s.nextInt();
boolean b = true;
for(int i = 2; i <= number / 2; i++){
if(number % i == 0){
b = false;
i = number;
}
}
if(b){
System.out.println(number + "is a prime number !");
} else {
System.out.println(number + "is not a prime number !");
}
}
}
3. 打印九九乘法表
public class ForForExer2 {
public static void main(String[] args){
for(int i = 1; i <= 9; i++){
for(int j =1; j <= i; j++){
System.out.print(j + "*" + i + "=" + i * j + '\t');
}
System.out.println();
}
}
}
4.打印直角三角形
public class ForForExer {
public static void main(String[] args){
/*
*
**
***
****
*/
for(int i = 1; i <= 4; i++){ //row
for(int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println();
}
/*
****
***
**
*
*/
for(int i = 4; i >= 1; i--){
for(int j = i; j >= 1; j--){
System.out.print("*");
}
System.out.println();
}
}
}
break和continue
- break:用于选择或者循环语句中,表示结束当前一层语句
- continue:用于循环语句中,表示跳出当前循环继续下次循环
这两个关键字必须结合相应的语句使用,单独存在没有意义,因此单独存在的时候编译报错
循环支持标号形式。当需要一次性跳出多层循环的时候,可以使用标号形式。但是不推荐,降低了程序的阅读性。