Java嵌套循环问题 [英] Java nested loop question

查看:106
本文介绍了Java嵌套循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种程序中,我理解如果j> i程序进入外循环,并且它增加i以便它可以返回但是当它返回j时应该是2,因为我们在我们回到外循环之前增加它但是我已经看到当我们回到j时是1而不是2,我不明白wh



我尝试了什么:



On this kind of program i understand that if the j>i the program goes to the outer loop, and it increments i so it can go back in but when it goes back in the j is supposed to be at 2 because we incremented it before we got back to the outer loop but as i've seen when we go back in j is at 1 and not 2, i do not understand wh

What I have tried:

import java.util.Scanner;
public class Exercise16 {

   public static void main(String[] args)

{
   int i,j,n;
   System.out.print("Input number of rows : ");
 Scanner in = new Scanner(System.in);
		    n = in.nextInt();

   for(i=1;i<=n;i++)
   {
	for(j=1;j<=i;j++)
	  System.out.print(j);

    System.out.println("");
    }
}
}

推荐答案

引用:

但是当它返回j时应该是2,因为我们在返回外循环之前递增它

but when it goes back in the j is supposed to be at 2 because we incremented it before we got back to the outer loop

Nope。这是一个错误的假设。实际上, j 再次是 1 ,因为在每次外循环迭代时,内循环初始化部分执行 j = 1 )。

Nope. That is a wrong assumption. In fact, j is 1 again, because, at every outer loop iteration, the inner loop initialization part (j=1) is executed.


引用:

我不明白wh



您的代码没有按照您的预期行事,或者您不明白为什么!

只是看它预制棒。



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http:// docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器仅用于显示代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


Your code do not behave the way you expect, or you don't understand why !
Just watch it preform.

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于Java嵌套循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆