3.SpringBoot學習(三)——WebMVC及其工作原理
阿新 • • 發佈:2020-07-12
Java流程控制
使用者互動scanner
java.util.Scanner獲取使用者的輸入(Java5的新特徵)
import java.util.Scanner;
Scanner s = new Scanner(System.in);
next()
- 一定要讀到有效字元之後才可以結束輸入
- 對輸入有效字元之前遇到的空白,next()方法會自動將其去掉
- 只有輸入有效字元之後才將其後面輸入的空白作為分隔符或者結束符
- next()不能得到帶有空格的字串
Scanner scanner = new Scanner(System.in); System.out.println("使用next方法接收:"); if(scanner.hasNext()){ String str = scanner.next(); System.out.println("輸入的內容為:"+str); } //下面是自己練習的時候發現:輸入Hello World的話,上下兩個分別輸出Hello和World,沒有空格,可以理解成,上面的next接收之後,scanner會把多餘的空格和剩餘的單詞保留下來;第二次接收的時候會對剩下的內容進行接收,但還是會省略空格;若是下面改成nextLine接收,則會出現 World含空格。 if(scanner.hasNext()){ String str = scanner.next(); System.out.println("輸入的內容為:"+str); } scanner.close(); //凡是屬於IO流的類如果不關閉會一直佔用資源,要養成良好的習慣用完就關掉。
nextLine()
- 以Enter為結束符
- 可以獲得帶有空格的字元
Scanner scanner = new Scanner(System.in); System.out.printlin("使用nextLine方法接收:"); if(scanner.hasNext()){ String str = scanner.nextLine(); System.out.println("輸入的內容為:"+str); } //自己練習的時候發現,如果上一次將全部輸入都用光的話,下一次執行會要求使用者再次輸入,以進行新的輸出 if(scanner.hasNext()){ String str2 = scanner.nextLine(); System.out.println("輸入的內容為:"+str2); } //問題:scanner每次被輸入的時候會被重新整理嗎?就是重新輸入的時候scanner會清零,但是仍舊識別為有文字的狀態? scanner.close();
練習1
瞎搞的判斷
public static void main(Sring[] 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); }esle{ System.out.println("輸入的不是整數!"); } System.out.println("請輸入小數:"); if(scanner.hasNextFloat()){ f = scanner.nextFloat(); System.out.println("輸入的小數為:"+f); }esle{ System.out.println("輸入的不是小數!"); } }
練習2
輸入多個數字,求其總和與平均數,每輸入一個數字用回車確認,通過輸入非數字來結束輸入併發出執行結果
public static void main(String[] args){
Scanner scanner = 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(m+"個數字的和為:"+sum);
System.out.println(m+"個數字的平均值為:"+(sum/m));
}
順序結構
Java的基本結構就是順序結構
選擇結構
if單選澤結構(不含else)/雙選擇結構(含else)
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入內容:");
String s = scanner.nextLine();
if(s.equals("Hello")){
System.out.println("Hello");
}else {
System.out.println("End");
scanner.close();
}
System.out.println("-------------------------------------------------------------")
Scanner scanner = new Scanner(System.in);
System.out.println("Please input your score:");
int score = scanner.nextInt();
if(score>=60){
System.out.println("Congratulations!You pass the examination!");
}else{
System.out.println("What a pity!You don't pass the examination!");
}
if多選結構(else if)
Scanner scanner = new Scanner(System.in);
System.out.println("Please input your score:");
int score = scanner.nextInt();
if(score == 100){
System.out.println("Congratulations!You have killed it!");
}else if (score>=90 && score<100){
System.out.println("Congratulations!Your class is A!");
}else if (score>=80 && score<90){
System.out.println("Congratulations!Your class is B!");
}
else if (score>=0 && score<80){
System.out.println("Your class is C!");
}else{
System.out.println("Illegal input!");
}
scanner.close();
巢狀的if結構
switch多選擇結構
//case穿透 沒有break會被擊穿的
char grade = 'C';
switch (grade){
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Good!");
break;
case 'C':
System.out.println("OK!");
break;
case 'D':
System.out.println("Alright!");
break;
case 'F':
System.out.println("Alright!");
break;
default:
System.out.println("None.");
從JDK7開始 支援字串型別了
String name = "Faye";
//反編譯 java->class 通過IDEA進行反編譯 class丟進來
switch (name){
case "Faye":
System.out.println("Godness!");
break;
default:
System.out.println("Nobody!");
break;
迴圈結構
while迴圈
//輸出1~100
int i = 0;
while(i<100){
i++;
System.out.println(i);
}
do···while迴圈:即使不滿足條件,也至少執行一次
int i = 0;
int sum = 0;
do{
i++;
sum = sum+i;
}while(i<100);
System.out.println(sum);
for迴圈
int a = 1;//初始化條件
while (a<=100){//條件判斷
System.out.println(a);//迴圈體
a+=2;//迭代
}
System.out.println("while迴圈結束");
//
for(int i = 1;i<=100;i++){
System.out.println(i);
}
System.out.println("for迴圈結束");
/*
* 最先執行的初始化步驟,可以宣告一種型別,但可以初始化一個或多個迴圈控制變數,也可以是空語句。
* 然後,檢測布林表示式的值,如果為true,迴圈體被執行,如果為false,迴圈終止,開始執行迴圈體後面的語句
* 執行一次迴圈後,更新迴圈控制變數
* 再次檢測布林表示式,迴圈執行上面的過程
* */
System.out.println("-------------------------------------------------------------")
int s1 = 0;
int s2 = 0;
for (int i = 0;i<=100;i++){
if (i%2==0){
s2 = i+s2;
}else{
s1 = i+s1;
}
}
System.out.println("奇數和為:"+s1);
System.out.println("偶數和為:"+s2);
System.out.println("-------------------------------------------------------------")
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");//println輸出之後會換行 print輸出之後不會換行
}
if (i%15 == 0){
System.out.println("\n");
}
System.out.println("-------------------------------------------------------------")
/*列印第一列
* 用一個j把迴圈包起來
* 去掉重複
* 調整樣式
* */
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
System.out.println("-------------------------------------------------------------")
int[] numbers = {10,20,30,40,50};//定義了一個數組
for (int i = 0;i<5;i++){
System.out.print(numbers[i]+"\t");
}
System.out.print("\n");
//遍歷陣列元素
for (int x:numbers){
System.out.println(x);
}
增強for迴圈
break&continue
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i == 30){
break;//強制跳出整個迴圈,不執行剩餘的語句
}
}
System.out.println("-------------------------------------------------------------")
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;//用於終止某次迴圈
}
System.out.print(i+"\t");
}
練習
列印三角形
//列印三角形
public static void main(String[] args) {
for (int i = 1;i<=5;i++){
for (int j = 5;j>=i; j--){
System.out.print(" ");
}
for (int j = 1;j<=i;j++){
System.out.print("*");
}
for (int j = 1;j<i;j++){
System.out.print("*");
}
System.out.println();
}
}