作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
public class inc {
public static void main (String []args) {
inc.sixOrSeven();
}
public static void sixOrSeven() {
byte q=5;
if (q==5)
q+=1;
else
q--;
q++;
System.out.println(q);
}
}
我是计算机科学的新手,我的老师教了我一些关于增量和减量的概念。当我运行程序时,控制台显示答案是 7。但我开始怀疑答案本身。由于q=5
5+1=6
并且如果q不是5那么5+1-1=5
?答案怎么会是7呢。
最佳答案
如果你写的是 6:
public static void sixOrSeven() {
byte q=5;
if (q==5) {
q+=1;
}
else {
q--;
q++;
}
System.out.println(q);
}
根据规范:
和statements ,可写为:
Statement:
StatementWithoutTrailingSubstatement
(...)
StatementWithoutTrailingSubstatement:
Block
(...)
(...)
所以我们最终得到:
if (Expression)
single statement (can be a block)
else
single statement (can be a block)
关于java - 不带大括号的 if-else 行为 : How is the answer 7 and not 6?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41114448/
我是一名优秀的程序员,十分优秀!