程式設計日記:Java學習之路(四)
4.迴圈語句
4.1迴圈語句:
當某個條件滿足時反覆執行一段程式碼塊,直到這個條件不滿足時跳出迴圈繼續執行後面的程式碼。
(1)while語句
// ①while ( 布林表示式 ) { // ②} // ③
(2)do-while語句
// ①do { // ②} while ( 布林表示式 );// ③
while語句和do-while語句在使用時的區別:
當第一次布林表示式的值為false時:
while語句的迴圈體一次也不執行
do-while語句的迴圈體會被執行一次
while語句存在的問題:
處理迴圈變數的程式碼分散在程式各處,程式碼不夠緊湊。
(3)for語句
for ( inti = 0; i < 10; i++ ) { // 程式碼塊}
while語句和for語句在使用時的區別:
while語句適用於無法確定次數的迴圈
只需要指定判斷條件
讀檔案中的資料
for語句適用於可以確定次數的迴圈
需要指定迴圈次數(起點、終點、遞增值)
累加計算
巢狀的迴圈語句:
迴圈語句可以互相巢狀
迴圈語句可以和分支語句互相巢狀
迴圈變數的作用範圍:只在迴圈體內有效。
在寫一個迴圈語句之前需要注意三件事情:
(1)迴圈變數:執行次數
(2)迴圈條件:在什麼時候退出
(3)迴圈體:
4.2 控制語句:
·break語句
·continue語句
·return語句
break語句:
在switch語句中,跳出switch語句執行後面的程式碼
在迴圈語句中,跳出迴圈執行後面的程式碼
配合if語句使用,當滿足某一條件時跳出迴圈
在巢狀的迴圈語句中,配合標籤跳出指定的迴圈語句
break語句只能跳出離自己最近的迴圈語句,如果要跳出多個迴圈語句,需要配合標籤使用
for ( int i = 1; i < 9; i++ ) { for ( int j = 1; j < 9; j++ ) { if ( j > 5 ) { break; } } }
loop: for ( int i = 1; i < 9; i++ ) {for ( int j = 1; j < 9; j++ ) { if ( j > 5 ) { break loop; } } }
continue語句:
在迴圈語句中,跳出本次迴圈重新判斷迴圈條件
配合if語句使用,當滿足某一條件時跳出本次迴圈
在巢狀的迴圈語句中,配合標籤跳出指定的迴圈語句的本次迴圈過程
同break語句
return語句:
退出函式
如果函式為主函式,則退出程式
死迴圈:
while ( true ) { // 程式碼塊} for ( ; ; ) { // 程式碼塊}
死迴圈不是錯誤,有些程式需要死迴圈。
如果一個迴圈中包含break語句,就不是死迴圈。
專案需求:
實現一個ATM,程式啟動後要求輸入密碼,如果密碼正確,則進入主介面,否則再次要求輸入密碼,連續三次輸入錯誤則退出程式。
在主介面中有四個選項,分別為查詢餘額、存款、取款和退出,使用者選擇相應的功能後會有對應的提示引導使用者進行操作,如果使用者選擇退出,則退出本程式,否則再次顯示主介面。
程式碼:
import java.util.Scanner; public class Java_day04_02_ATM { public static void main(String[] args){ //初始化模組 int password = 123456789; double BALANCE = 10000.0; System.out.println("請輸入密碼:"); int use_password1=0; for(int i = 0;i<3;i++){ Scanner s = new Scanner(System.in); int passworduse = s.nextInt(); if(passworduse==password){ while (true) { System.out.print("1.餘額查詢 "); System.out.print("2.存款 "); System.out.print("3.取款 "); System.out.println("4.退出"); Scanner q = new Scanner(System.in); int XuanXiang = q.nextInt(); switch (XuanXiang) { case 1: System.out.println("您的餘額為:"+BALANCE);break; case 2: System.out.println("請輸入存款金額:"); Scanner w = new Scanner(System.in); int cq = w.nextInt(); if(cq>=0) { BALANCE += cq; System.out.println("存款成功,您的餘額為:" + BALANCE); } else{ System.out.println("輸入金額不符,存款失敗"); } break; case 3: System.out.println("請輸入取款金額:"); Scanner e = new Scanner(System.in); int qk = e.nextInt(); BALANCE -= qk; if(qk>=0 || qk<=BALANCE){ System.out.println("取款成功,您的餘額為:"+BALANCE); }else{ System.out.println("輸入金額不符,或餘額不足,取款失敗"); } break; case 4: System.out.println("感謝您的使用,再見"); return; } } } if(i<=1){ System.out.println("密碼輸入錯誤"); }else { System.out.println("輸入密碼錯誤三次,程式退出"); } } } }
練習:
(1)從鍵盤輸入兩個整數,要求計算兩個整數之間所有整數的和。
import java.util.Scanner; public class Java_day04_03 { public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.print("請輸入第一個數:"); int a = s.nextInt(); System.out.print("請輸入第二個數:"); int b = s.nextInt(); int sum = 0; for(int i = a+1;i<b;i++){ sum += i; } System.out.print("這兩個數之間整數的和為:"+sum); } }
(2)從鍵盤輸入一個整數m,輸出使用“*”組成的邊長為m的三角形。例如m = 3時,輸出:
import java.util.Scanner; public class Java_day04_04 { public static void main(String[] args){ Scanner s = new Scanner(System.in); int n = s.nextInt(); for(int i = 0;i<n;i++){ for(int j = 0;j<=i;j++){ System.out.print("*"); } System.out.println(); } } }
擴充套件:
關鍵字:final指示常量(main方法內)
final double CM_PER_INCH=2.54;
static final設定類常量(main方法外)
public static final double CM_PER_INCH=2.54
我們可以自定義列舉型別,列舉型別包括有限個命名的值
enum Size{small,medium,large}; Size s = Size.small
四捨五入運算:
(int)Math.round(x)
這裡仍然要進行強制型別轉換,因為round方法返回的是long型別,由於存在資訊丟失的可能性,所以只有使用顯示的強制型別轉換,才能夠將long型別轉換為int型別
字串是不可被修改的,但是可以拼接
greeting = greeting.substring(0,3)+"p"