Java流程控制
用户交互Scanner
1. java.util.Scanner (java5新特性)
1. Scanner scanner = new Scanner(System.in);
2. scanner.next()/scanner.nextline()
3. IO类使用完之后记得关闭
顺序结构
1.按照顺序执行 依次执行 基本数据结构
选择结构
1.if单选择 双选择 多选择 嵌套
2.switch 多选择
循环结构
1.while循环
2.do..while循环
3.for循环
4.增强for循环 type x: X
break & continue
1.goto的类似方法 continue后面可以跟一个leble (慎用 最好别用)
package scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接受纪念盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接受:");
//判断用户有没有输入字符串
if (scanner.hasNext()){
String str = scanner.next();
System.out.println("输出的内容为:"+str);
}
//整行输入输出
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("输出的内容为:"+str);
}
//参数输入
int i = 0;
float f = 0.11F;
if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println(i);
}else{
System.out.println("输入不是证书!");
}
//凡是属于IO流的嘞如果不关闭会一直占用资源,好习惯用完就关掉
scanner.close();
}
}
package scanner;
import java.util.Scanner;
public class Demo02 {
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++;
sum += x ;
}
System.out.println("m: "+m);
System.out.println("sum/m: " + (sum/m) );
scanner.close();
}
}
package scanner;
public class Demo03 {
public static void main(String[] args) {
String name = "ZAC";
switch (name) {
case "ZAC" -> System.out.println("ZAC");
case "ZAA" -> System.out.println("ZAA");
default -> System.out.println("default");
}
}
}
标签:控制,Java,scanner,流程,System,String,println,out,Scanner
From: https://www.cnblogs.com/dmbj/p/17525236.html