d********o 发帖数: 1738 | 1 下面这段代码的结果为什么不是
See no evil.
而是
See no evil.
Speak no evil.
Hear no evil.
public class Advice {
public final static int LITTLE_ADVICE = 0;
public final static int MORE_ADVICE = 1;
public final static int LOTS_OF_ADVICE = 2;
public static void main(String[] args) {
dispenseAdvice(LOTS_OF_ADVICE);
}
public static void dispenseAdvice(int howMuchAdvice) {
switch(howMuchAdvice) { // (1)
case 2:
System.ou |
g*****g 发帖数: 34805 | 2 If you don't break in each case, it will follow through.
【在 d********o 的大作中提到】 : 下面这段代码的结果为什么不是 : See no evil. : 而是 : See no evil. : Speak no evil. : Hear no evil. : public class Advice { : public final static int LITTLE_ADVICE = 0; : public final static int MORE_ADVICE = 1; : public final static int LOTS_OF_ADVICE = 2;
|
a**e 发帖数: 5794 | 3 在这方面C#和Perl设计得更合理。
【在 g*****g 的大作中提到】 : If you don't break in each case, it will follow through.
|
d********o 发帖数: 1738 | 4 有点不明白,LOTS_OF_ADVICE的值自始至终是2, 为什么语句(3)和语句(4)也执行
了。什么是fallow through?follow through 是不是将所有的Case语句执行一遍!
case 1: System.out.println("Speak no evil.");也执行了, 难道说LOTS_OF_ADVICE
变量值会自动递减!
【在 g*****g 的大作中提到】 : If you don't break in each case, it will follow through.
|
w*****g 发帖数: 1415 | 5 他发现第一个Match之后,以后就不再比较,直接执行了,除非碰到Break; |
g*****g 发帖数: 34805 | 6 If you ever learn assembly, you'll understand why this is natural.
"case blahblah" is a just a label. You can have as many labels as
you like but the flow won't change until you do jump ("break").
C used this design and C++, Java followed.
ADVICE
【在 d********o 的大作中提到】 : 有点不明白,LOTS_OF_ADVICE的值自始至终是2, 为什么语句(3)和语句(4)也执行 : 了。什么是fallow through?follow through 是不是将所有的Case语句执行一遍! : case 1: System.out.println("Speak no evil.");也执行了, 难道说LOTS_OF_ADVICE : 变量值会自动递减!
|
d********o 发帖数: 1738 | 7 Thank you so much!
【在 w*****g 的大作中提到】 : 他发现第一个Match之后,以后就不再比较,直接执行了,除非碰到Break;
|
d********o 发帖数: 1738 | 8 终于明白了,谢谢!
我以前用foxpro, 一看到switch,就想到do case语句,形成偷懒的习惯了!
【在 g*****g 的大作中提到】 : If you ever learn assembly, you'll understand why this is natural. : "case blahblah" is a just a label. You can have as many labels as : you like but the flow won't change until you do jump ("break"). : C used this design and C++, Java followed. : : ADVICE
|
l*******G 发帖数: 1191 | 9 matlab does not need breaks, yet many need, so it is language-dependent, you should be very
carefull |