Java流程控制
1.用戶互動Scanner
(1)實作程式和人的互動,java.util.Scanner,可以用Scanner類來獲取用戶的輸入
? 基本語法:Scanner sc = new Scanner(System.in);
(通過Scanner類的next()與nextLine()方法獲取輸入的字串,在讀取之前,我們一般用hasNext()與hasNextLine()判斷是否還有輸入的資料,
- next():
- 一定讀取到有效字符后才可以結束輸入
- 對輸入的有效字符之前遇到的空白,next()方法會自動將其去掉,
- 只有輸入有效字符后才將其后面輸入的空白作為分隔符或者結束符,
- next()不能得到帶有空格的字串,

- nextLine():
- 以Enter為結束符,也就是說nextLine()方法回傳的是輸入回車之前的所有字符,
- 可以獲得空白,

不用if陳述句判斷:

Scanner進階使用
package com.kuang.scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//我們可以輸入多個數字,并求其總和與平均數,每輸入一個數字用回車確認,通過輸入非數字來結束并輸出運算結果
Scanner scanner = new Scanner(System.in);
double sum = 0; //和
int m = 0; //m個數
System.out.println("請輸入資料:");
//通過回圈判斷是否還有輸入,并在里面對每一次進行求和統計
while (scanner.hasNextDouble()) {
double x = scanner.nextDouble();
m = m + 1; //m++
sum = sum + x;
System.out.println("你輸入了第" + m + "個數,當前的結果sum=" + sum);
}
System.out.println(m + "個數字和為:" + sum);
System.out.println(m + "個數字的平均數是:" + (sum / m));
scanner.close();
}
}
2.順序結構
? Java的基本結構,依次執行,他是任何一個演算法都離不開的基本結構,
3.選擇結構
-
if單選擇結構
if (布爾運算式) {如果布爾運算式為true將執行的陳述句} -
if雙選擇結構
-
if多選擇結構
package com.kuang.struct;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
//多條件判斷,判斷成績等級
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = scanner.nextInt();
if (score == 100) {
System.out.println("滿分");
} else if (score<100 && score>=90) {
System.out.println("A");
} else if (score<90 && score>=80) {
System.out.println("B");
} else if (score<80 && score>=70) {
System.out.println("C");
} else if (score<70 && score>=60) {
System.out.println("D");
} else if (score<60 && score>=0) {
System.out.println("不及格");
} else {
System.out.println("成績不合法");
}
scanner.close();
}
}
-
嵌套的if結構
-
switch多選擇結構(break可選):判斷一個變數與一系列值中某個值是否相等,每個值稱為一個分支
switch陳述句中的變數型別可以是byte、short、int或char,從JavaSE 7開始switch支持字串String型別,
case穿透現象 :必須加上break;
package com.kuang.struct;
public class SwitchDemo01 {
public static void main(String[] args) {
char grade = 'C';
//case穿透,switch匹配一個具體的值
switch (grade) {
case 'A':
System.out.println("優秀");
break; //break可選,不加break,會發生穿透現象!!!
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
default:
System.out.println("未知等級");
}
}
}
? 反編譯 java---class(位元組碼檔案)---反編譯(idea)
? 反編譯,查看原始碼:hashCode(每一個物件都有其對應的hashCode,判斷哈希值是不是相同)

4.回圈結構
- while回圈
- 只要布爾運算式為true,回圈就會一直執行下去(死回圈)
- 大多數情況都需要一個運算式失效的方式來結束回圈
- 少部分情況需要一直回圈,比如服務器的請求回應監聽等
- 盡量避免死回圈,會造成程式性能或者程式卡死奔潰
package com.kuang.struct;
public class WhileDemo03 {
public static void main(String[] args) {
//計算1+2+3+4...+100=?
//高斯
int i = 0;
int sum = 0;
while (i <= 100) {
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
- do...while回圈:至少執行一次

- for回圈
package com.kuang.struct;
public class ForDemo01 {
public static void main(String[] args) {
for (int i=1; i<100; i++) {
System.out.println(i);
}
/*
關于for回圈有以下幾點說明:
最先執行初始化步驟,可以宣告一種型別,但可初始化一個或多個回圈控制變數,也可以是空陳述句,
然后,檢測布爾運算式的值,如果為true,回圈體被執行,如果為false,回圈終止,繼續執行后面的陳述句,
執行一次回圈后,更新回圈控制變數(迭代因子控制著回圈變數的增減)
再次執行布爾運算式,回圈執行上面的程序,
*/
//死回圈
for (;;){
}
}
}
練習1
計算1-100之間所有奇數和偶數的和,
package com.kuang.struct;
public class ForDemo02 {
public static void main(String[] args) {
//計算0-100之間奇數和偶數的和
int oddSum = 0; //偶數
int evenSum = 0; //奇數
for (int i = 0; i <= 100; i++) {
if (i%2 != 0) {
oddSum += i;
} else{
evenSum += i;
}
}
System.out.println(oddSum);
System.out.println(evenSum);
}
}
練習2
用while或for回圈輸出1-1000之間能被5整除的數,并且一行輸出3個數
package com.kuang.struct;
public class ForDemo03 {
public static void main(String[] args) {
//用while或for回圈輸出1-1000之間能被5整除的數,并且一行輸出3個數
for (int i = 1; i <= 1000; i++) {
if (i%5==0) {
System.out.print(i + "\t");
}
if (i%(5*3)==0){ //5、10、15,一行中的最后一個數能被15整除,則一行3個數,換行
System.out.println();
}
}
System.out.println();
System.out.println("============================");
//用while回圈
int a = 1;
while (a<=1000) {
a++;
if (a%5==0) {
System.out.print(a + "\t");
}
if (a%(5*3)==0) {
System.out.println();
}
}
}
}
練習3:九九乘法表
package com.kuang.struct;
/*
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
public class ForDemo04 {
public static void main(String[] args) {
//列印九九乘法表
/*
1.我們先列印第一列
2.用一個回圈抱起來
3.去掉重復項
4.調整樣式
*/
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + "\t");
}
System.out.println();
}
/*for (int y = 1; y <= 9; y++) {
for (int x = 1; x <= y; x++) {
System.out.print(x + "*" + y + "=" + (y * x) + "\t");
}
System.out.println();
}*/
}
}
詳細情況可用斷點查看,
-
增強for回圈:java5引入了一種主要用于陣列或集合的增強型for回圈
格式:
for(宣告陳述句:表達值){ //代碼句子; }宣告陳述句:宣告新的區域變數,該變數的型別必須和陣列元素型別匹配,其作用域限定在回圈陳述句塊,其值與此時陣列元素的值相等,
運算式:運算式是要訪問的陣列名,或者是回傳值為陣列的方法,
package com.kuang.struct;
/*
for(宣告陳述句:表達值){
//代碼句子;
}
*/
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};
for (int i=0; i<5; i++){
System.out.println(numbers[i]);
}
System.out.println("==================");
//遍歷陣列元素
//增強for回圈
for (int x:numbers) { //相當于把numbers陣列中的元素全部賦值給了x
System.out.println(x);
}
}
}
5.break&continue
- [ ] break在任何回圈陳述句的主體部分,均可用break控制回圈的流程,break用于強行跳出回圈,不執行回圈陳述句中剩余的陳述句,
package com.kuang.struct;
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
break;
}
System.out.println(i);
}
}
}
- [ ] continue陳述句用在回圈陳述句體中,用于終止某次回圈程序,即跳出回圈體中尚未執行的陳述句,接著進行下一次是否執行回圈的判定,
package com.kuang.struct;
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println(" ");
continue;
}
System.out.print(i + " ");
}
}
}
-
[ ] goto關鍵字,帶標簽的break和continue,標簽是指后面跟一個冒號的識別符號,例如label:
在java中唯一用到標簽的地方是在回圈陳述句之前,而在回圈陳述句之前設定標簽的唯一理由是:我們希望在其中嵌套另一個回圈,由于break和continue關鍵字通常指中斷當前回圈,但若隨標簽使用,他們就會中斷到存在標簽的地方,
package com.kuang.struct;
public class LabelDemo {
public static void main(String[] args) {
//列印101-150之間的質數
int count = 0;
outer:for (int i=101; i<150; i++) {
for (int j=2; j<i/2; j++){
if (i%j ==0){
continue outer;
}
}
System.out.print(i + " ");
}
}
}
6.練習
- 列印三角形 5行
package com.kuang.struct;
public class TestDemo {
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();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/304803.html
標籤:Java
