Java流程控制
一、Scanner对象
用于获取用户的输入
1.基本语法
Scanner s = new Scanner(System.in);
next() | 获取输入的字符串 |
---|---|
nextLine() | 获取输入的字符串 |
hasNext() | 判断是否还有输入数据 |
hasNextLine() | 判断是否还有输入数据 |
1.1 next()
用next判断输入信息(不完整):
- 读取到有效字符后才能结束
- 自动删除输入有效字符之前遇到的空白
- 输入有效字符后,将后面的空白作为结束符
- 不能读取带有空白字符串
package com.shen.BasicJava;
import java.util.Scanner;
public class Prc {
public static void main(String[] args) {
//create a scanner to scan input
Scanner s = new Scanner(System.in);
System.out.println("use the 'next' to receive: ");
//Check whether the user entered a string
if (s.hasNext()){
//receive the string
String str = s.next();
System.out.println("text is " + str);
}
// release the resources
s.close();
}
}
1.2 nextLine()——常用
- 以Enter为结束
- 可以获得空白
用nextLine()判断输入的完整信息:
package com.shen.BasicJava;
import java.util.Scanner;
public class Prc {
public static void main(String[] args) {
//create a scanner to scan input
Scanner s = new Scanner(System.in);
System.out.println("use the 'nextLine' to receive: ");
//Check whether the user entered a string
if (s.hasNextLine()){
//receive the string
String str = s.nextLine();
System.out.println("text is " + str);
}
// release the resources
s.close();
}
}
2. 其他应用
整数和浮点数的判断
package com.shen.BasicJava;
import java.util.Scanner;
public class Prc {
public static void main(String[] args) {
int i = 0;
float f = 0.0f;
//create a scanner to scan input
Scanner s = new Scanner(System.in);
System.out.println("please input a int");
//Check whether the user entered a Int
if (s.hasNextInt()){
//receive the Int
i = s.nextInt();
System.out.println("Int is " + i);
}else{
System.out.println("It`s not a int");
}
// release the resources
System.out.println("please input a float");
//Check whether the user entered a float
if (s.hasNextFloat()){
//receive a float
f = s.nextFloat();
System.out.println("Float is " + f);
}else{
System.out.println("It`s not a float");
}
s.close();
}
}
3. 实例
- 题目:输入多个数字求和求平均值,每输入一个数字用回车确认,通过输入非数字来结束输入并输出结果
package com.shen.BasicJava;
import java.util.Scanner;
public class Prc {
//题目:输入多个数字求和求平均值,每输入一个数字用回车确认,通过输入非数字来结束输入并输出结果
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = 0;
double a = 0;
double sum = 0;
double ave = 0;
//用while判断是否还有double类输入
while (scanner.hasNextDouble()){
a = scanner.nextDouble();
i = i+1; //++i;
sum = sum + a;
System.out.println("已输入" + i + "个数据");
}
ave = sum/i;
System.out.println("The result of sum is " + sum);
System.out.println("The result of average is " + ave);
scanner.close();
}
}
二、顺序结构
java中的基础结构
三、选择结构
3.1 if单选结构
语法
if(boolean){
//if the boolean is true execute statement
}
3.2 if双选结构
语法
if(boolean){
//if the boolean is true
}else{
//if the boolean is false
}
3.3 if多选结构
语法
if(boolean1){
//if the boolean1 is true
}else if(boolean2){
//if the boolean2 is true
}else if(boolean3){
//if the boolean3 is true
}else{
//else
}
3.4 嵌套的if结构
语法
if(boolean1){
//if boolean1 is true
if(boolean2){
//if boolean2 is true
}
}
3.5 switch多选结构
- switch case语句判断一个变量是否与一系列中的某个值相等,每个值为一个分支。
- 语法
switch(expression){
case value :
//语句
break;//可选
case value :
//语句
break;//可选
default ://可选
//语句
}