[英]Trouble figuring out how to return this recursive function?
New to the java programming language and confused how to run this code! Java编程语言的新手,对如何运行此代码感到困惑! I am using Eclipse.
我正在使用Eclipse。
public static String random(String n)
{
int F = n.length();
if(F <= 1) return n;
String b = n.substring(0, F/2);
String c = n.substring(F/2, F);
return random(c) + random(b);
}
I am new to running java programs in the terminal window and I can't seem to figure out how to get the return value. 我是在终端窗口中运行Java程序的新手,但似乎无法弄清楚如何获取返回值。
Put it into a class like this 放在这样的班上
public class MyRandom {
public static void main(String[] args) {
System.out.println(random("abc"));
}
public static String random(String n) {
int length = n.length();
if (length <= 1)
return n;
String b = n.substring(0, length / 2);
String c = n.substring(length / 2, length);
return random(c) + random(b);
}
}
and then right click on the file and choose "Run As" 然后右键单击该文件,然后选择“运行方式”
You can use the following code in your main
method: 您可以在
main
方法中使用以下代码:
System.out.println(random("Hello"));
Since random
method returns a String
, you can print it out in the terminal. 由于
random
方法返回String
,因此您可以在终端中将其打印出来。
将对函数的调用包装在标准输出中,如下所示: System.out.println(random("test"));
String returnValue = random("YourNString");
So the return value will be stored in returnValue
所以返回值将存储在
returnValue
Then your can do System.out.println(returnValue)
然后你可以做
System.out.println(returnValue)
Or simply do System.out.println(random("YourNString"));
或者干脆做
System.out.println(random("YourNString"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.