- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
class Foozit {
public static void main(String[] args) {
Integer x = 0;
Integer y = 0;
for (Short z = 0; z < 5; z++) {
if ((++x > 2) || ++y > 2)
x++;
}
System.out.println(x + "Hello World!" + y);
}
}
我尝试了这段 scjp 代码,得到的输出是 5 3 谁能告诉我哪里出错了
最佳答案
循环执行5次(z从0到4)
在 if 条件下,++x 被计算了五次。但是只有当条件的第一部分为假时,++y 才会被评估。
即,这种情况:
if ((++x > 2) || ++y > 2)
变成:
//1st iteration
if( 1 > 2 || 1 > 2 ) //False, x++ is not evaluated
//2nd iteration
if( 2 > 2 || 2 > 2 ) //False, x++ is not evaluated
//3rd iteration
if( 3 > 2 || 2 > 2 ) //True, the second ++y is skipped, but x++ is evaluated, x becomes 4
//4th iteration
if( 5 > 2 || 2 > 2 ) //True, the second ++y is skipped, but x++ is evaluated, x becomes 6
//5th iteration
if( 7 > 2 || 2 > 2 ) //True, the second ++y is skipped, but x++ is evaluated, x becomes 8
最后,我们有:
x = 8 and y = 2
记住:++x 是预递增(想想改变和使用)而 x++ 是后递增(想想使用和改变)
关于java - SCJP程序给出输出8 2怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11433516/
我之前遇到的关于 SO 的问题已经将近 2 年了。 我刚刚为 SCJP 6 购买了 Kathy Sierra 和 Bert Bates 的书。但它发生了,所以现在在我阅读博客时,我听说 SCJP“PL
嗨,我无法理解为什么输出是 'ex [6, 7, b, d] [6, 7, b]' 这段代码。请有人建议子集如何与 数字和字母?谢谢 import java.util.*; public class
在调试java多线程程序时,我设置了断点。在启动方法调用后,控件将不会运行menthod,您可以让我知道调试过程吗? 示例代码 class Test extends Thread { publ
public class Hotel { private int roomNr; public Hotel(int roomNr) { this.roomNr = ro
下面将因“标签 z 丢失”而编译失败,但如果我只是将 z: 移动到 o = o + 2 之后的下面一步,那么这会起作用吗?这背后的逻辑是什么? public class Breaker { stati
考虑: class Building { Building() { System.out.print("b "); } Building(String name
public class Starter extends Thread{ private int x = 2; public static void main(String[]
给出下面的代码。该代码由“java Test 1 2 3 4”运行。结果是什么? public class Test { public static void main(String args[])
另一个让我困惑的考试问题: public String makinStrings() { String s = “Fred”; s = s + “47”; s = s.substring(2,
我在理解 K&B 的 SCJP 书中《面向对象》一章中的问题 9 时遇到问题。 问题: public class Redwood extends Tree { public static void m
即使得到正确答案,我也无法弄清楚 SCJP 问题: 从以下代码(来源:http://scjptest.com)中,我们需要确定何时引用为 myInt 的对象符合垃圾回收条件: 01.public vo
给定: public class LineUp { public static void main(String[] args) { double d = 12.345;
只是想知道 SUN 是否提供任何可用于 SCJP 的好东西(免费或付费)??我听说微软正在提供这样的好东西。有人对此有任何想法吗? 干杯,对战 最佳答案 你可以把它写在你的简历上。大部分就是这样。 肯
这个问题来自 SCJP 转储。可能看起来很愚蠢,但我对选项有点困惑。请帮帮我 public class Donkey2 { public static void main(String[] ar
public class Person { private String name; public Person(String name) { this.name =
实际上来自SCJP 6 StudyGuide Exam310-065(MGH,2008) alt text http://www.freeimagehosting.net/uploads/1f3746
我无法理解 SCJP 书籍 K&B 第 9 章(线程)中的以下程序 问题: class Dudes{ static long flag = 0; // insert code her
Kathey Sierra 的 SCJP 书中摘录如下: If a method is overridden but you use a polymorphic (supertype) referen
public class Threads2 implements Runnable { public void run() { System.out.println("run.
这个问题是由考试实验室操纵的 public class B{ public static void main(String args[]){ byte g =10;
我是一名优秀的程序员,十分优秀!