控制流程
與任何程式設計語言一樣,Java使用條件陳述句和回圈結構確定控制流程,
塊作用域
我們首先要了解塊(block)的概念,
塊是指由若干條Java陳述句組成的陳述句,并用一對大括號括起來,塊確定了變數的作用域,一個塊可以嵌套在另一個快中,下面就是嵌套在main方法塊中的一個塊,
public static void main(String[] args) {
int n = 1;
{
int k = 3;
System.out.println(k); // success
} // k只在這個塊中被定義
System.out.println(k); // error
}
但是,不能在嵌套的兩個塊中宣告同名的變數,否則就會有錯誤,無法通過編譯:
public static void main(String[] args) {
int n = 1;
{
int k = 3;
int n = 2;
}
}
這里已經在外面定義了n,就不能再嵌套的塊中再定義n了,
條件陳述句
在Java中,條件陳述句的形式為
if (condition) statement
這里的條件必須用小括號括起來,剩下的跟其他語言語法幾乎一直,直接看以下例子
import java.util.Scanner;
public class SecondSample {
public static void main(String[] args) {
/*
* 根據銷售額來評價你的表現,獎勵你不同的金額
* */
// 創建輸入物件
Scanner in = new Scanner(System.in);
System.out.println("請輸入你的銷售額");
// 在控制臺輸入你的銷售額
int yourSales = in.nextInt();
// 定義一個目標
int target = 1000;
// 初始化表現
String performance;
// 初始化獎金
int bonus;
if (yourSales >= 2 * target) {
performance = "優秀";
bonus = 1000;
System.out.printf("你的表現為%s,獎勵你%d元%n", performance, bonus);
} else if (yourSales >= 1.5 * target) {
performance = "良好";
bonus = 500;
System.out.printf("你的表現為%s,獎勵你%d元", performance, bonus);
} else if (yourSales >= target) {
performance = "及格";
bonus = 100;
System.out.printf("你的表現為%s,獎勵你%d元", performance, bonus);
} else {
System.out.println("You 're fired");
}
}
}
while回圈
當條件為true時,while回圈執行下一條陳述句,一般形式如下:
while (condition) statement
我們設定一個程式,計算需要多長時間才能夠存盤一定數量的退休金,假定每年存入相同數量的金額,而且利率是固定的,
import java.util.Scanner;
public class ThirdSample {
public static void main(String[] args) {
/*
* 計算需要多長時間才能夠存盤一定數量的退休金
* */
Scanner in = new Scanner(System.in);
System.out.println("你需要多少退休金?");
double goal = in.nextDouble();
System.out.println("你每年將增加多少錢?");
double payment = in.nextDouble();
System.out.println("利率是多少:");
double interestRate = in.nextDouble();
double balance = 0;
int years = 0;
// 未達到目標時更新帳戶余額
while (balance < goal) {
// 加上今年的付款和利息
balance += payment;
double interest = balance * interestRate / 100;
balance += interest;
years++;
}
System.out.printf("你可以在%d年內退休", years);
}
}
while回圈是先判斷后執行,如果條件不滿足則永遠是false,那么可能永遠不會執行,如果我們想無論條件是否為true,我們都要先執行一條陳述句,那么在Java中提供了do..while...這種回圈形式,
do statement while (condition)
下面的例子中,首先計算退休賬戶中的新的余額,然后再詢問是否打算退休:
public class ThirdSample {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("你每年將增加多少錢?");
double payment = in.nextDouble();
System.out.println("利率是多少?");
double interestRate = in.nextDouble();
double balance = 0;
int year = 0;
String input;
do {
balance += payment;
double interest = balance * interestRate / 100;
balance += interest;
year++;
System.out.printf("%d年后,你的余額為%,.2f%n", year, balance);
System.out.println("準備退休?(Y/N)");
input = in.next();
}
while (input.equals("N"));
}
}
for確定回圈
??for回圈陳述句是支持迭代的一種通用結構,由一個計數器或類似的變數控制迭代次數,每次迭代后這個變數將會更新,
for (int i=1; i<=10; i++){
System.out.println(i);
}
??for陳述句的第1部分通常是對計數器初始化;第2部分給出每次新一輪回圈執行前要檢測的回圈條件;第3部分指定如何更新計數器,
與C++一樣,盡管Java允許在for回圈的各個部分放置任何運算式,但有一條不成文的規則:for陳述句的3個部分應該對同一個計數器變數進行初始化、檢測和更新,若不遵守這一規則,撰寫的回圈常常晦澀難懂,
注意:在回圈中,檢測兩個浮點數是否相等需要格外小心,for (double x=0;x!=10;x+=0.1),這條陳述句永遠不會結束,由于舍入的誤差,永遠達不到精確的最終值,因為0.1無法精確地用二進制表示,所以,x將從9.999 999 999 999 98跳到10.099 999 999 999 98,
多重選擇:switch陳述句
在處理多個選項時,使用if/else陳述句就顯得有些笨拙,Java有一個與C/C++完全一樣的switch陳述句,例如,下面包含4個選項的選單系統
public class FifthSample {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Select an option (1, 2, 3, 4)");
int choice = in.nextInt();
switch (choice)
{
case 1:
System.out.println("我選擇了1");
break;
case 2:
System.out.println("我選擇了2");
break;
case 3:
System.out.println("我選擇了3");
break;
case 4:
System.out.println("我選擇了4");
break;
default:
System.out.println("默認選擇");
break;
}
}
}
switch陳述句將從與選項值相匹配的case標簽開始執行,直到遇到break陳述句,或者執行到switch陳述句的結束處為止,如果沒有相匹配的case標簽,而有default子句,就執行這個子句,
注意:強烈不建議使用switch陳述句,最好永遠不要使用,因為如果在case分支陳述句的末尾沒有break陳述句,那么就會接著執行下一個case分支陳述句,這種情況跟相當危險,常常會引發錯誤,
case的標簽可以是:
- 型別為char、byte、short或int常量運算式
- 列舉常量
- 從Java7開始,case標簽還可以是字串字面量
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499406.html
標籤:Java
上一篇:java面試題(2022最新)
下一篇:Tomcat介紹和配置使用
