加法变为字符串拼接,结果为字符串。故最后为字符串"10"
和202.0
的拼接。
见上
public class Demo {
public static void main(String args[]) {
System.out.println(inc(10) + inc(8) + inc(-10)) ; // 20 + 16 - 1
}
public static int inc(int temp) {
if (temp > 0) {
return temp * 2 ;
}
return -1 ;
}
}
如果为正数,返回参数的2倍值;如果不是正数,返回-1。结果为20 + 16 + (-1)
public class Demo {
public static void main(String args[]) {
char c = 'A' ;
int num = 10 ;
switch(c) {
case 'B' :
num ++ ;
case 'A' :
// 匹配成功,开始执行
num ++ ; // num = 11
case 'Y' :
num ++ ; // num = 12
break ;
// 因break跳出switch
default :
num -- ;
}
System.out.println(num) ;
}
}
switch-case语句块中break的重要性
public class Demo {
public static void main(String args[]) {
int sum = 0 ;
for (int x = 1 ; x < 10 ; x ++) {
sum += x ;
if (x % 3 == 0) {
continue ;
}
}
System.out.println(sum) ;
}
}
感觉这道题sum += x
的位置可能写错了,应该在if的后面,要么就只是单纯的和下一道题作对比。现在这段代码里if的用处几乎没有,结果和没有if时是一样的,都是1+2+…+9=45。
public class Demo {
public static void main(String args[]) {
int sum = 0 ;
for (int x = 0 ; x < 10 ; x ++) {
sum += x ;
if (x % 3 == 0) {
break ;
}
}
System.out.println(sum) ;
}
}
和上一题类似,不过i的初始值变成了0,if里面的continue变成了break。由于0对3取余为0,所以直接跳出循环,输出sum的值0。