2.1 比較
注釋:
- 以兩個(gè)斜杠“//”開頭的語(yǔ)句,注釋(comment)插入在程序代碼中,用來(lái)向讀者提供解釋信息。它們對(duì)于程序的功能沒(méi)有任何影響,但是往往能使得程序更容易被人類讀者理解。
-
延續(xù)數(shù)行的注釋,要用多行注釋的格式來(lái)寫。多行注釋由一對(duì)字符序列“
”開始,而以“
”結(jié)束。也可用于一行內(nèi)的注釋。
2.1.1 關(guān)系運(yùn)算
-
計(jì)算兩個(gè)值之間的關(guān)系,所以叫關(guān)系運(yùn)算
關(guān)系運(yùn)算-Java.png - 當(dāng)兩個(gè)值的關(guān)系符合關(guān)系運(yùn)算符的預(yù)期時(shí),關(guān)系運(yùn)算的結(jié)果為true,否則為false
System.out.println(5==3); //結(jié)果為true
System.out.println(5>3); //結(jié)果為true
System.out.println(5<=3); //結(jié)果為false
2.1.2 優(yōu)先級(jí)
- 所有的關(guān)系運(yùn)算符的優(yōu)先級(jí)比算術(shù)運(yùn)算的低,但是比賦值運(yùn)算的高
6 > 1
5 == 5
7 >= 3 + 4
- 判斷是否相等的==和!=的優(yōu)先級(jí)比其他的低,而連續(xù)的關(guān)系運(yùn)算是從左到右進(jìn)行的
5 > 3 == 6 > 4
6 > 5 > 4
a == b == true
a == b == 6
a == b > false
(a == b) > false
- int vs double
int a = 5;
double b = 5.0;
System.out.println(a == b); //結(jié)果是false
double a = 1.0;
double b = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
System.out.println(a == b); //結(jié)果是false,因?yàn)閎的值實(shí)際上是0.999
可用Math.abs(f1-f2) < 0.00001來(lái)規(guī)避
2.2 判斷
2.2.1 if語(yǔ)句
if語(yǔ)句這一行結(jié)束的時(shí)候并沒(méi)有表示語(yǔ)句結(jié)束的“;”,而后面的賦值語(yǔ)句寫在if的下一行,并且縮進(jìn)了,在這一行結(jié)束的時(shí)候有一個(gè)語(yǔ)句結(jié)束的“;”。這表明這條賦值語(yǔ)句是if語(yǔ)句的一部分,if語(yǔ)句擁有和控制這條賦值語(yǔ)句,決定它是否需要執(zhí)行。
if(total > amount)
total += amount + 10;
例1:
final int MINOR = 35;
System.out.print("請(qǐng)輸入你的年齡:");
Scanner in = new Scanner(System.in);
int age = in.nextInt();
System.out.print("你的年齡是"+age);
if(age < MINOR)
{
System.out.println("年輕是美好的,");
}
System.out.println("年齡決定了你的精神世界,好好珍惜吧。");
2.2.2 流程圖
以特定的圖形符號(hào)加上說(shuō)明,表示算法的圖,稱為流程圖。
- 圓角矩形表示“開始”與“結(jié)束”。
- 矩形表示行動(dòng)方案、普通工作環(huán)節(jié)用
- 菱形表示問(wèn)題判斷或判定(審核/審批/評(píng)審)環(huán)節(jié)
- 用平行四邊形表示輸入輸出
- 箭頭代表工作流方向

2.2.3 比較數(shù)的大小
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
int max;
if(x > y)
max = x;
System.out.println(max);
它沒(méi)有解決y大于x的問(wèn)題,當(dāng)x>y的條件不成立時(shí),程序就結(jié)束了,max沒(méi)有得到值。
方案1
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
int max;
if(x > y)
max = x;
if(y > x)
max = y;
System.out.println(max);
方案2
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
int max = y;
if(x > y)
max = x;
System.out.println(max);
if-else
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
int max;
if(x > y)
max = x;
else
max = y;
System.out.println(max);
例2:
final int PASS=60;
Scanner scan = new Scanner(System.in);
System.out.print("請(qǐng)輸入成績(jī): ");
int score = scan.nextInt();
System.out.println("你輸入的成績(jī)是"+score);
if ( score < PASS )
System.out.println("很遺憾,這個(gè)成績(jī)沒(méi)有及格。");
else
System.out.println("祝賀你,這個(gè)成績(jī)及格了。");
System.out.println("再見(jiàn)");
2.2.4 嵌套和級(jí)聯(lián)的if
例3:
找三個(gè)數(shù)中的最大

嵌套的判斷
當(dāng)if的條件滿足或者不滿足的時(shí)候要執(zhí)行的語(yǔ)句也可以是一條if或if-else語(yǔ)句,這就是嵌套的if語(yǔ)句
if(code == READY)
if(count < 20)
System.out.println("一切正常\n");
else
System.out.println("繼續(xù)等待\n");
else的匹配
else總是和最近的那個(gè)if匹配
if(code == READY){
if(count < 20)
System.out.println("一切正常\n");
}else
System.out.println("繼續(xù)等待\n");
tips
- 在if或else后面總是用{}
- 即使只有一條語(yǔ)句的時(shí)候
2.2.5 級(jí)聯(lián)的if-else-if
if(exp1)
st1;
else if(exp2)
st2;
else
st3;
例4:分段函數(shù)
單一出口
//單一出口,好
int f;
if(x < 0){
f = -1;
} else if(x == 0){
f = 0;
}else {
f = 2 * x;
}
System.out.println(f);
//這樣不好
if ( x < 0 ) {
System.out.println(-1);
} else if ( x == 0 ) {
System.out.println(0);
} else {
System.out.println(2 * x);
}
if語(yǔ)句常見(jiàn)的錯(cuò)誤
- 忘了大括號(hào)
- if后面的分號(hào)
- 錯(cuò)誤使用==和=
- 使人困惑的else
代碼風(fēng)格 - 在if和else之后必須加上大括號(hào)形成語(yǔ)句塊;
- 大括號(hào)內(nèi)的語(yǔ)句縮進(jìn)一個(gè)tab的位置。
2.3 分支
2.3.1 switch-case
if ( type==1 )
System.out.println(“你好”);
else if ( type==2)
System.out.println("早上好");
else if ( type==3 )
System.out.println("晚上好");
else if ( type==4 )
System.out.println("再見(jiàn)");
else
System.out.println("啊,什么?。?);
switch ( type ) {
case 1:
System.out.println("你好");
break;
case 2:
System.out.println("早上好");
break;
case 3:
System.out.println("晚上好");
break;
case 4:
System.out.println("再見(jiàn)");
break;
default:
System.out.println("啊,什么?。?);
}
- 控制表達(dá)式只能是整數(shù)型的結(jié)果
- 常量可以是常數(shù),也可以是常數(shù)計(jì)算的表達(dá)式
- 根據(jù)表達(dá)式的結(jié)果,尋找匹配的case,并執(zhí)行case后面的語(yǔ)句,一直到break為止
- 如果所有的case都不匹配,那么就default后面的語(yǔ)句;如果沒(méi)有default,那么就設(shè)么都不做
switch (控制表達(dá)式) {
case 常量:
語(yǔ)句
……
case 常量:
語(yǔ)句
……
default:
語(yǔ)句
……
}
2.3.2 break
switch語(yǔ)句可以看作是一種基于計(jì)算的跳轉(zhuǎn),計(jì)算控制表達(dá)式的值后,程序會(huì)跳轉(zhuǎn)到相匹配的case(分支標(biāo)號(hào))處。分支標(biāo)號(hào)只是說(shuō)明switch內(nèi)部位置的路標(biāo),在執(zhí)行完分支中的最后一條語(yǔ)句后,如果后面沒(méi)有break,就會(huì)順序執(zhí)行到下面的case里去,直到遇到一個(gè)break,或者switch結(jié)束為止。
習(xí)題
1 時(shí)間轉(zhuǎn)換(5分)
題目?jī)?nèi)容:
UTC是世界協(xié)調(diào)時(shí),BJT是北京時(shí)間,UTC時(shí)間相當(dāng)于BJT減去8?,F(xiàn)在,你的程序要讀入一個(gè)整數(shù),表示BJT的時(shí)和分。整數(shù)的個(gè)位和十位表示分,百位和千位表示小時(shí)。如果小時(shí)小于10,則沒(méi)有千位部分;如果小時(shí)是0,則沒(méi)有百位部分;如果分小于10分,需要保留十位上的0。如1124表示11點(diǎn)24分,而905表示9點(diǎn)5分,36表示0點(diǎn)36分,7表示0點(diǎn)7分。
有效的輸入范圍是0到2359,即你的程序不可能從測(cè)試服務(wù)器讀到0到2359以外的輸入數(shù)據(jù)。
你的程序要輸出這個(gè)時(shí)間對(duì)應(yīng)的UTC時(shí)間,輸出的格式和輸入的相同,即輸出一個(gè)整數(shù),表示UTC的時(shí)和分。整數(shù)的個(gè)位和十位表示分,百位和千位表示小時(shí)。如果小時(shí)小于10,則沒(méi)有千位部分;如果小時(shí)是0,則沒(méi)有百位部分;如果分小于10分,需要保留十位上的0。
提醒:要小心跨日的換算。輸入格式:
一個(gè)整數(shù),表示BJT的時(shí)和分。整數(shù)的個(gè)位和十位表示分,百位和千位表示小時(shí)。如果小時(shí)小于10,則沒(méi)有千位部分;如果小時(shí)是0,則沒(méi)有百位部分;如果小時(shí)不是0而且分小于10分,需要保留十位上的0。
輸出格式:
一個(gè)整數(shù),表示UTC的時(shí)和分。整數(shù)的個(gè)位和十位表示分,百位和千位表示小時(shí)。如果小時(shí)小于10,則沒(méi)有千位部分;如果小時(shí)是0,則沒(méi)有百位部分;如果小時(shí)不是0而且分小于10分,需要保留十位上的0。
輸入樣例:
933
輸出樣例:
133
思路
略。
代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int B = in.nextInt();
int U = B - 800;
if(U < 0) {
U += 2400;
}
System.out.println(U);
}
}
結(jié)果

2 信號(hào)報(bào)告(5分)
題目?jī)?nèi)容:
無(wú)線電臺(tái)的RS制信號(hào)報(bào)告是由三兩個(gè)部分組成的:
R(Readability) 信號(hào)可辨度即清晰度.
S(Strength) 信號(hào)強(qiáng)度即大小.
其中R位于報(bào)告第一位,共分5級(jí),用1—5數(shù)字表示.
1---Unreadable
2---Barely readable, occasional words distinguishable
3---Readable with considerable difficulty
4---Readable with practically no difficulty
5---Perfectly readable
報(bào)告第二位是S,共分九個(gè)級(jí)別,用1—9中的一位數(shù)字表示
1---Faint signals, barely perceptible
2---Very weak signals
3---Weak signals
4---Fair signals
5---Fairly good signals
6---Good signals
7---Moderately strong signals
8---Strong signals
9---Extremely strong signals
現(xiàn)在,你的程序要讀入一個(gè)信號(hào)報(bào)告的數(shù)字,然后輸出對(duì)應(yīng)的含義。如讀到59,則輸出:
Extremely strong signals, perfectly readable.輸入格式:
一個(gè)整數(shù),信號(hào)報(bào)告。整數(shù)的十位部分表示可辨度,個(gè)位部分表示強(qiáng)度。輸入的整數(shù)范圍是[11,59]內(nèi)有效的數(shù)字,這個(gè)范圍外的數(shù)字不可能出現(xiàn)在測(cè)試數(shù)據(jù)中。
輸出格式:
一句話,表示這個(gè)信號(hào)報(bào)告的意義。按照題目中的文字,先輸出表示強(qiáng)度的文字,跟上逗號(hào)和空格,然后是表示可辨度的文字,跟上句號(hào)。注意可辨度的句子的第一個(gè)字母是小寫的。注意這里的標(biāo)點(diǎn)符號(hào)都是英文的。
輸入樣例:
33
輸出樣例:
Weak signals, readable with considerable difficulty.
思路
略。
代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int RS = in.nextInt();
int R = RS / 10;
int S = RS % 10;
switch(S) {
case 1:
System.out.print("Faint signals, barely perceptible, ");
break;
case 2:
System.out.print("Very weak signals, ");
break;
case 3:
System.out.print("Weak signals, ");
break;
case 4:
System.out.print("Fair signals, ");
break;
case 5:
System.out.print("Fairly good signals, ");
break;
case 6:
System.out.print("Good signals, ");
break;
case 7:
System.out.print("Moderately strong signals, ");
break;
case 8:
System.out.print("Strong signals, ");
break;
case 9:
System.out.print("Extremely strong signals, ");
break;
}
switch(R) {
case 1:
System.out.println("unreadable.");
break;
case 2:
System.out.println("barely readable, occasional words distinguishable.");
break;
case 3:
System.out.println("readable with considerable difficulty.");
break;
case 4:
System.out.println("readable with practically no difficulty.");
break;
case 5:
System.out.println("perfectly readable.");
}
}
}
結(jié)果

---END---
