如何打印多行输出?并纠正我的递归方法? [英] How do I printing multiple lines of output? And correct my recursive method?

查看:62
本文介绍了如何打印多行输出?并纠正我的递归方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写此代码的目的是使用3个递归方法将3行作为输出。

第1行1,2,3,4,5,5,6,...,n ,

第二行1,4,9,...,n * n,

第三行2,4,6,8,10,...,n ,



但我不知道哪里不正确?以及如何分隔每一行?



我尝试过:



my purpose to write this code ist that to have 3 line as output with 3 recursive method.
first line 1,2,3,4,5,5,6,..,n,
second line 1,4,9,...,n*n,
third line 2,4,6,8,10,...,n,

but I do not know where is incorrect? and how to separate every line?

What I have tried:

public class Recursiv {

	

	private static int prt1234(long n) {
		if (n == 1) {

			return 1;
		} else {

			return prt1234(n-1);

		}
	}

	

	private static int prtSqr1234(long n) {

		if (n == 0) {

			return 0;
		} else {

			return (int) (prtSqr1234(n - 1) - 1 + (2 * n));

		}
	}

	private static int prt2468(long n) {

		if (n % 2 == 0) {
			if (n <= 2) {

				return 2;
			} else {

				return prt2468(n - 2);

			}
		} else {
			n = n - 1;
			if (n <= 2) {

				return 2;
			} else {

				return prt2468(n - 2);

			}

		}

	}
	public static void main(String[] args) {
		Recursiv f = new Recursiv();
		for(long i = 1; i < Long.parseLong(args[0]); i++) {
            System.out.print( f.prt1234(i)+ ",");
        
        }
		
	        for(long j = 1; j < Long.parseLong(args[0]); j++) {
	            System.out.printf( prtSqr1234(j)+ ",");
	        
	        }
	        for(long k = 1; k < Long.parseLong(args[0]); k++) {
	            System.out.print( f.prt2468(k)+ ",");
	        
	        }
		
		
	}
}







第二个解决方案




Second solution

public class Recursive {

	private static int prtSqr1234(long m) {

		return (int) (m == 0 ? 0 : prtSqr1234(m - 1) + m + m - 1);

	}

	public static void main(String[] args) {

		for (long j = 1; j <= Long.parseLong(args[0]); j++) {
			System.out.print(prtSqr1234(j) + ",");
		}

	}
}

推荐答案

Quote:

并更正我的递归方法?



更正代码是你的工作,调试器是帮助你的工具。

你可以自由地创建任意复杂的代码,但是不要指望我们这样跟着你。

为什么你需要递归方法?


你想要的


prt1234(1)= 1

prt1234(2 )= 2

prt1234(3)= 3

prt1234(4)= 4

prt1234(5)= 5

问题是没有人会有想法看看递归方法来回答这个问题,因为有一个更简单的解决方案。



你的代码没有表现你期望的方式,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。 />
调试器是她的e向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

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

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



a href =https://en.wikipedia.org/wiki/Debugger>调试器 - 维基百科,免费的百科全书[ ^ ]



掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



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

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [< a href =https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html\"target =_ blanktitle =New Window> ^ ]



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


Correcting your code is your job, the debugger is the tool that will help you to.
You are free to create code as complicated as you want, but do not expect us to try to follow you this way.
Why do you need recursive methods ?

you want:
prt1234(1)=1
prt1234(2)=2
prt1234(3)=3
prt1234(4)=4
prt1234(5)=5
The problem is that nobody will have the idea to look at recursive method to answer this, because there is a much simpler solution.

Your code do not behave the way you expect, or you don't understand why !

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 code 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.

a href="https://en.wikipedia.org/wiki/Debugger">Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

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.


这篇关于如何打印多行输出?并纠正我的递归方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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