Java Processing Control(流程控制)
-
user interaction Scanner (用户交互scanner)
-
sequential structrue (顺序结构)
-
case structrue(选择结构)
-
loop structrue(循环结构)
-
break&continue
-
practice
Scanner
java.util.Scanner
通过Scanner类来获取用户的输入
- 基本语法:
Scanner s = new Scanner(System.in)
- 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用 hasNext()与hasNextLine()判断是否还有输入的数据
package com.example.demo08;
import java.util.Scanner;
public class demo08 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收: ");
//判断用户有没有输入字符串
if(scanner.hasNext()){
//use next method to receive
String str = scanner.nextLine();
System.out.println("输出人内容为:" + str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close();
}
}
package com.example.demo08;
import java.util.Scanner;
public class demo08 {
public static void main(String[] args) {
//we can input so many words,then get sum&avg, using enter key to confirm each num u input
//to end input by inputting non-numeric num then execute output result
Scanner scanner = new Scanner(System.in);
//sum
double sum = 0;
//calculate how much num has inputted
int m = 0;
//verify if there is any input by loop then count for each sum;
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = m+1;//m++
sum = sum + x;
}
System.out.println(m+"个数的和为" + sum);
System.out.println(m+"个数的平均值是" + (sum/m));
scanner.close();
}
}
顺序结构
-
JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行
-
顺序结构是最简单的算法结构
-
语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构
选择结构
- if单选择结构
- if双选择结构
- if多选择结构
- 嵌套的if结构
- switch多选择结构
if单选择结构
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("please input some content: ");
String s = scanner.nextLine();
//equals to confirm if there is any equal String
if(s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
if双选择结构
public static void main(String[] args) {
//a score greater than 60 marks a passing grade,if less than 60 marks a failed grade
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩: ");
int score = scanner.nextInt();
if(score > 60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
if多选择结构
Switch 多选择结构
-
switch case here to verify a variable which equals to a series of values, and called each value branch
switch (expression){ case value : //sentence break;//alternative case value : //sentence break;//alternative default : //alternative //sentence }
- Switch,the variable in switch could be:
- byte/short/int/char
- begin with SE 7
- String was supported in Switch
- meanwhile case table must be String constants & literals
- Switch,the variable in switch could be:
public class demo08 {
//case 穿透 //switch 匹配一个具体的值
public static void main(String[] args) {
char grade = 'c';
switch(grade){
case 'A':
System.out.println("perfect");
break;
case 'B':
System.out.println("excellent");
case 'c':
System.out.println("good");
break;
default:
System.out.println("unknown rank");
}
}
标签:scanner,Day03,System,String,println,课堂,随笔,out,Scanner
From: https://www.cnblogs.com/chenwenge/p/16650882.html