?? java入門(7).htm
字號:
<P> ______________________________________________________________________</P>
<P> ______________________________________________________________________</P>
<P> b.使用for語句:</P>
<P> _____________________________________________________________________</P>
<P> ______________________________________________________________________</P>
<P> ______________________________________________________________________</P>
<P> ______________________________________________________________________</P>
<P> ______________________________________________________________________</P>
<P> ______________________________________________________________________</P>
<P> 10)在下列程序片段中,你認為變量condition是________型的。你認為這個程序存在什么問題?</P>
<P> a.int型 b.boolean型 c.float型 d.String型</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
while (condition)
{
y++;
x-=5;
}
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> ______________________________________________________________________</P>
<P> ______________________________________________________________________</P>
<P> ______________________________________________________________________</P>
<P> 11)判斷以下命題是否正確,如果不正確,請舉例說明。</P>
<P> for循環都能夠改寫成while循環……………………………………………()</P>
<P> while循環都能夠改寫成for循環……………………………………………()</P>
<P> ______________________________________________________________________</P>
<P> ______________________________________________________________________</P>
<P> ______________________________________________________________________</P>
<P><B> 練習答案</B></P>
<P> 1)b if語句是Java語言中的分支結構流控制語句。</P>
<P> 2)b 如果循環語句中,控制循環結構的條件表達式永遠為真值的話,整個程序將陷入死循環。這是最重要的問題。</P>
<P> 3)c x每減一次5,y就累加1,x從250減到0,得減50次,因此y最終值為50。</P>
<P> 4)a 只要x大于0,循環就不會結束, 250每次減去5,當x不大于0時,值為0。</P>
<P> 5)c d 循環體與y的值相等,50次;而要確定不執行循環體時,還要做一次判斷,所以,共判斷了51次。</P>
<P> 6) b 每一次循環,都是以執行循環條件表達式開始的,因此是x>0</P>
<P> 7) c 在for循環結束的最后一句,應該執行修改表達式,修改循環變量的值。</P>
<P> 8) 按這樣的修改后,程序就會從8開始打印,9、10、11……,直到231-1。為什么會這樣呢?這是因為:</P>
<P> § counterLoop的初值是8,大于0,因此執行循環體;</P>
<P> § 在循環體中,又將counterLoop加上了1,得到了9;</P>
<P> § 這樣9顯然也是大于0,再次執行循環體。</P>
<P> 這樣,counterLoop的值越來越大,也就再次運行循環體。</P>
<P> 那么,為什么到了231-1就結束了呢?這是因為,int的最大值是231-1,超過這個數以后,就溢出,而溢出就小于0,循環結束。</P>
<P> 這個程序就算是死循環了,循環能夠結束,只是因為數字溢出。</P>
<P> 9) 使用while循環:</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
public class whileLianxi
{
String weeks[ ] = {“Monday”,”Tuesday”,”Wednsday”,
”Thursday”,”Friday”,”Saturday”,”Sunday”};
int x=0;
public static void main(String args[])
{
while(x<7)
{
System.out.println(weeks[x]);
x++;
}
}
}
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> 而如果使用for語句來實現的話,就是:</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
public class forLianxi
{
String weeks[ ] = {“Monday”,”Tuesday”,”Wednsday”,
”Thursday”,”Friday”,”Saturday”,”Sunday”};
int x;
public static void main(String args[])
{
for(x=0;x<7;x++)
{
System.out.println(weeks[x]);
}
}
}
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> 10)b 能夠做為條件表達式的變量,只能是boolean型的。</P>
<P> 在這個程序中,循環體內并未改變condition的值。如果condition是true的話,則這個循環將永遠不會結束;而如果condition是fasle的話,還好,只是不執行這個循環體。</P>
<P> 11)for循環都能夠改寫成while循環……………………………………………(對)</P>
<P> while循環都能夠改寫成for循環……………………………………………(錯)</P>
<P> 例如,上一題中的while循環就無法改寫成為for循環。</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
while (condition)
{
y++;
x-=5;
}
</CODE></PRE></TD></TR></TBODY></TABLE>
<P><B> 7.3 多分支結構</B></P>
<P><B> 實例說明</B></P>
<P> 在前面的章節中,我們學習過了使用if語句的分支結構,那么多分支結構又有什么作用呢?下面,我們思考一下如何完成這樣一個任務:</P>
<P> 我們要構造一個程序,讓它判斷我的成績myScope,如果:</P>
<P> 1) 如果大于90分,打印“Excellent”(優秀);</P>
<P> 2) 如果大于80分,小于90分,打印“Good”(很好);</P>
<P> 3) 如果大于70分,小于80分,打印“Average”(一般);</P>
<P> 4) 如果大于60分,小于70分,打印“Pool”(差);</P>
<P> 5) 如果小于60分,打印“Failure”(極差)。</P>
<P> 如果要使用學習過的if語句來完成,將會十分費勁,程序也十分難懂。下面我們做一個實踐:</P>
<P> 1.首先,我們使用文字編輯軟件輸入下源程序。</P>
<P> 源程序:multiCass.java</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
public class multiCase
{
public static void main(String args[])
{
int myScope=89;
int grade=myScope/10;
switch(grade)
{
case 10:
case 9:
System.out.println(“Excellent!”);
break;
case 8:
System.out.println(“Good!”);
break;
case 7:
System.out.println(“Average!”);
break;
case 6:
System.out.println(“Pool”);
break;
default:
System.out.println(“Failure!”);
}
}
}
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> 2.使用javac編譯這個程序,并運行這個程序。我們將得到如下的輸出結果:</P><A
href="Java入門(7).files/7-9.jpg"><IMG alt=7-9
src="Java入門(7).files/7-9.jpg" width=450 border=0></A>
<P><B> 圖7-9 程序multiCass.java的輸出</B></P>
<P><B> 傳授新知</B></P>
<P> 這個程序比較長,不過還是很好理解的,它可以分成兩個邏輯塊:</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
int myScope=89;
int grade=myScope/10;
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> 首先,定義了變量myScope,用來存放我的成績;然后定義了一個整型變量grade,它的值是myScope的十分之一。</P>
<P><B> 一些提示:</B></P>
<P> 在這里涉及了一個編程的技巧,我們將grade賦值為myScope/10,這樣,我們就可以得到myScope的十位上的數(由于是整型變量,所以不會有小數),通過這個十位上的數就可以簡單地得到它們的分數段。</P>
<P> 接下來,就是本節的主角,多分支結構控制語句switch-case:</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
switch(grade)
{
case 10:
case 9:
System.out.println(“Excellent!”);
break;
case 8:
System.out.println(“Good!”);
break;
case 7:
System.out.println(“Average!”);
break;
case 6:
System.out.println(“Pool”);
break;
default:
System.out.println(“Failure!”);
}
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> switch,開關,我們定義了一個grade開關,然后根據它的狀態來決定執行什么語句:</P>
<P> 1) 當(case)開關值為10和9時,則執行語句:</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
System.out.println(“Excellent!”);
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> 2) 而當開關值為8時,則執行語句:</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
System.out.println(“Good!”);
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> 3) 而當開關值為7時,則執行語句:</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
System.out.println(“Average!”);
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> 4) 當開關值為6時,則執行語句:</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
System.out.println(“Pool!”);
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> 5) 如果開關值為其它的話,就視為default,執行語句:</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%"
bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
System.out.println(“Failure!”);
</CODE></PRE></TD></TR></TBODY></TABLE>
<P> 注意,在每種case之間,都使用了break語句,break,在英文中是中斷的意思,也就是說,執行到這里中斷多分支結構,跳出switch-case語句。</P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -