📄 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 + -