简体   繁体   English

有人可以告诉我这里的Java代码在做什么吗?

[英]Can someone please explain to me what the java code here is doing?

I am learning java using the headfirst Java book. 我正在使用第一本Java书籍来学习Java。 I have a problem understanding what some Java do and how an output came to be. 我在理解某些Java功能以及输出结果方面遇到了问题。 For example: 例如:

class MultiFor {

 public static void main(String[] args) {
    // write your code here
        int x = 0;
        int y = 30;

        for (int outer = 0; outer < 3; outer++){
            for (int inner = 4; inner > 1; inner--){
                x = x + 3;
                y = y - 2;
                if (x == 6){
                    break;
                }
                x = x + 3;
            }
            y = y - 2;
        }
        System.out.println(x + " "  + y);
    }
}

my output is 54 6, but i don't know how it came to be. 我的输出是54 6,但我不知道结果如何。 Can someone explain this? 有人可以解释吗?

The For-Loops works that way: 循环工作方式如下:

for(initialisation ; condition; steps){ do Stuff } for(初始化;条件;步骤){做东西}

initialisation is the part where Variables are defined not necessary Variables for the condition but they are only reachable in the for-loop. 初始化是定义变量的部分,对于条件而言,变量不是必需的,但只能在for循环中访问。

condition as long as the condition is true the will run again. 只要条件为真,条件将再次运行。

steps mostly an interation for the loops in this case its outer = outer +1 and inner = inner -1. 在这种情况下,步骤主要是循环的一个外部,外部=外部+1,内部=内部-1。

in this example the outer for-loop runs from outer=0 to outer=2 and the inner runs from inner = 4 to inner = 2. In the If-Condition is tested on x == 6 after reaching this true status it will stop the inner for-loop with a break;. 在此示例中,外部for循环从外部= 0到外部= 2,内部从内部= 4到内部=2。在达到此真实状态后,如果在x == 6上测试If-Condition,则它将停止内部for循环有一个中断;

thats what it do how it reachs the expected values is just counting the loops and adding values. 多数民众赞成在做什么以达到预期值只是计算循环并添加值。

Value of x and y are initialized 0 and 30 respectively. x和y的值分别初始化为0和30。 There are two loops, inner and outer. 有两个循环,内部循环和外部循环。 Outer loop would iterate three times and the inner loop would execute three times for every iteration of outer loop, but have a break condition when x would be 6. Following are the values of x and y after each iteration of the inner and the outer loop. 对于外循环的每次迭代,外循环将迭代3次,而内循环将执行3次,但是当x为6时将具有中断条件。以下是内循环和外循环的每次迭代之后的x和y值。

Iteration outer:0 Iteration inner :4 x =3 x =6 y =28 Iteration inner :3 x =9 x =12 y =26 Iteration inner :2 x =15 x =18 y =24 外部迭代次数:0:内部迭代次数:4 x = 3 x = 6 y = 28内部迭代次数:3 x = 9 x = 12 y = 26内部迭代次数:2 x = 15 x = 18 y = 24

y =22

Iteration outer :1 Iteration inner :4 x =21 x =24 y =20 Iteration inner :3 x =27 x =30 y =18 Iteration inner :2 x =33 x =36 y =16 外部迭代:1内部迭代:4 x = 21 x = 24 y = 20内部迭代:3 x = 27 x = 30 y = 18内部迭代:2 x = 33 x = 36 y = 16

y =14

Iteration outer :2 Iteration inner :4 x =39 x =42 y =12 Iteration inner :3 x =45 x =48 y =10 Iteration inner :2 x =51 x =54 y =8 外部迭代:2内部迭代:4 x = 39 x = 42 y = 12内部迭代:3 x = 45 x = 48 y = 10内部迭代:2 x = 51 x = 54 y = 8

y =6

Hence the final value of x and y would be 54 and 6 respectively. 因此,x和y的最终值分别为54和6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM