Java流程控制 01 - 06


Java流程控制 01 - 06

Java流程控制01 - 用户交互Scanner

java.util.Scanner,我们可以根据Scanner类来获取用户输入

1
Scanner s = new Scanner(System.in);

通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。

next()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package scanner;

import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
// 创建一个scanner对象,用于接受键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收 : ");

if (scanner.hasNext()){
//使用next接收
String str = scanner.next();
System.out.println("输出内容为:"+str);
}
//凡是属于IO流的类,使用完记得关掉节约资源
scanner.close();
}
}
1
2
3
使用next方式接收 : 
hello world
输出内容为:hello

nextLine()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package scanner;

import java.util.Scanner;

public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextline方式接受 : ");

if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("输出内容为:"+str);
}
scanner.close();
}
}
1
2
3
使用nextline方式接受 : 
hello world
输出内容为:hello world
  • next()
    1. 一定要读取到有效字符后才可以结束输入
    2. 对输入有效字符之前遇到的空白,next()方法会自动把它去掉
    3. 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符
    4. next()不能得到带有空格的字符串
  • nextLine()
    1. 以Enter为结束符,也就是说nextLine()方法返回的是输入回车前的所有字符
    2. 可以获得空白

Java流程控制02 - Scanner进阶使用

判断输入类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package scanner;

import java.util.Scanner;

public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int i = 0;
float f = 0.0f;

System.out.println("请输入整数:");

if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整体数据" + i);
}
else {
System.out.println("输入的不是整数数据");
}
System.out.println("请输入小数");
if (scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小数数据"+f);
}
else{
System.out.println("输入的不是小数数据");
}
}
}

许输入多个数字,求总和和平均数,每输入一个数字用回车确认通过输入非数字来结束输入并输出结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package scanner;

import java.math.BigDecimal;
import java.util.Scanner;

public class Demo04 {
public static void main(String[] args) {
//允许输入多个数字,求总和和平均数,每输入一个数字用回车确认通过输入非数字来结束输入并输出结果

Scanner scanner = new Scanner(System.in);

//和
double sum = 0;
int m = 0;

//通过循环判断是否还有输入,在内部求和
while (scanner.hasNextDouble()) {
double x = scanner.nextDouble();
m = m + 1;
sum = sum + x;
}
System.out.println("平均数"+(sum/m));
System.out.println("和"+sum);
scanner.close();
}
}

Java流程控制03 - 用顺序结构

基本结构

Java流程控制04 - if选择结构

1
2
3
4
5
if(布尔表达式){
//如果表达式为true执行的语句
}else{
//如果表达式为false执行的语句
}

Java流程控制05 - Switch选择结构

语法

1
2
3
4
5
6
7
8
9
10
switch(expression){
case value:
//语句
break;//可选
case value:
//语句
break;//可选
default://可选
//语句
}

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package struct;

public class switchDemo01 {
public static void main(String[] args) {
char grade = 'C';

switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("再接再厉");
break;
case 'E':
System.out.println("挂科");
break;
default:
System.out.println("未知等级");
}

}

}
1
及格

注意:

如果没有加 break 则会造成 switch 穿透,一直执行下去到有break或者除了switch

Java流程控制06 - While循环

  • while 是最基本的循环,它的结构为:
1
2
3
while(布尔表达式){
//循环内容
}

例子

1
2
3
4
5
6
7
8
9
10
11
package struct;

public class whileDemo01 {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}

Author: Liang Junyi
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Liang Junyi !
  TOC