递归作业 [英] Recursion Homework
本文介绍了递归作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个作业问题我可能想多了,我需要使用递归来反转 Hello.
以便最终结果没有 .
I have a homework problem I am possibly overthinking, I need to reverse Hello.
using recursion so that the end result does not have a .
我目前的方法是:
public void foo(){
Scanner scan = new Scanner(system.in);
char c = scan.nextChar();
if (c!='.')
foo();
System.out.print(c);
}
这似乎是相反的输出,但它仍然有 .
.有人能指出我正确的方向来摆脱这个时期吗?
This seems to output the reverse, however it still has the .
. Can someone point me in the right direction to get rid of the period?
推荐答案
使用 '.'
而不是 "."
因为您正在比较 char
,不是 String
:
Use '.'
instead of "."
since you are comparing a char
, not a String
:
public void foo(){
Scanner scan = new Scanner(system.in);
char c = scan.nextChar();
if (c != '.') {
foo();
System.out.print(c);
}
}
还要注意,如果它是一个String
,则需要使用equals
来进行比较.==
或 !=
用于比较原始类型,例如 char
.
Also note that if it were a String
, you'd need to use equals
to do the comparison. ==
or !=
is used to compare primitive types, such as char
.
这篇关于递归作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文