模拟嵌套循环

编程入门 行业动态 更新时间:2024-10-23 13:29:26
本文介绍了模拟嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在一个初学者的编程书籍(无许可限制)有以下code,Java中动态创建嵌套的循环:

In a beginner's programming book (free licence) there was the following code, dynamically creating nested loops in Java:

import java.util.Scanner; public class RecursiveNestedLoops { public static int numberOfLoops; public static int numberOfIterations; public static int[] loops; public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("N = "); numberOfLoops = input.nextInt(); System.out.print("K = "); numberOfIterations = input.nextInt(); input.close(); loops = new int[numberOfLoops]; nestedLoops(0); } public static void nestedLoops(int currentLoop) { if (currentLoop == numberOfLoops) { printLoops(); return; } for (int counter=1;counter <= numberOfIterations;counter++) { loops[currentLoop] = counter; nestedLoops(currentLoop + 1); } } public static void printLoops() { for (int i = 0; i < numberOfLoops; i++) { System.out.printf("%d ", loops[i]); } System.out.println(); } }

当输入N = 2和K = 3,在屏幕上应印像[1,1],[1,2],[1,3],[2,1],[2,2] ,[2,3],[3,1],[3,2],[3,3](用换行等)。该项目工程的罚款。  然后我试图调试它,花了相当长的一段时间,试图了解究竟如何它的工作原理。我不能。我的问题:

When inputting N=2 and K=3, on the screen should be printed something like [1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3] (with newlines, etc). The program works fine. Then I tried to debug it and spent a quite some time trying to understand how exactly it works. I couldn't. My question:

---->为什么打印后[1,3]变量'curentLoop'变成'0'是事先'1'?

----> why after printing [1,3] the variable 'curentLoop' becomes '0' being beforehand '1' ?

另外: - >在我的调试器(Eclipse的内置)印刷[1,3]指针去年底'}'的方法梅开二度nestedLoops后(与'currentLoop,值为1),然后突然它开始执行for循环使用'currentLoop'= 0哪里的变量,从利用其值为0?为什么要去方法的末尾括号后,它开始执行的循环,没有任何方法调用的名字?

Also: -> In my debugger (Eclipse built-in) after printing [1,3] the pointer goes to the end '}' brace of the method 'nestedLoops' (with 'currentLoop' with value 1), and then suddenly it starts executing the for-loop with 'currentLoop' = 0. Where does the variable take its value '0' from? Why after going to the end brace of the method, it starts executing the 'for loop', without any call to the method's name?

这可能是一个很容易回答的一些你;我只是一个初学者。预先感谢您的帮助。

This could be a very easy question to some of you; I'm just a beginner. Thank you in advance for your help.

推荐答案

由于它以嵌套的循环递归调用。首先,它被称为以0那么它被称为具有1,然后用2时,它得到总的循环数,它开始执行循环(这就是所谓的递归终端条件)。但是,每次调用嵌套循环被放置在栈中,它执行K,然后返回并执行K-1,然后再打开,并执行K-2,然后再打开,并执行K-3一路下跌到k - K = 0

Because it's a recursive call to nested loops. first, it is called with 0. then it is called with 1. then with 2. when it gets to the number of total loops, it begins executing the loops (this is called the recursive terminal condition). however, each call to nested loops is placed on the stack, it executes k, then returns and executes k-1, then returns and executes k-2, then returns and executes k-3 all the way down to k - k = 0.

如果我是你,我会放置一个断点nestedloops()内本身的召唤,看着它被调用。然后,因为它是所谓,看它的方式工作回落。

if i were you, i would place a breakpoint on the call of nestedloops() inside itself and watch what it is being called with. then, as it is called, watch it work its way back down.

更多推荐

模拟嵌套循环

本文发布于:2023-10-31 06:01:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545222.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!