如何完成反向字符串?
所以我的老师告诉我制作一个字符串,使我输入的任何内容都以相反的顺序排列 (例如“你好”变成“ereht olleh”)。到目前为止,我在主体上工作,我想出了这个
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
(The input needs to be in here)}}
public static String rev(String str) {
String reversed = "hello there";
for (int i = 0; i < str.length(); i++) {
reversed = str.charAt(i) + reversed;
}
return reversed;
我现在的问题是,我需要在 public static void main(String[] args) 下放置什么才能使其工作。是的,我知道这是作业,我尝试通过这本书寻求帮助(没有运气)。我尝试在互联网上查找(由于我对更高级的方法知之甚少,所以没有运气)。我将不胜感激任何帮助并提前感谢任何人。 如果有帮助的话我使用netbeans。
So my teacher told me to make a string that makes whatever I type go in the opposite order
(e.g. "hello there" becomes "ereht olleh"). So far I worked on the body and I came up with this
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
(The input needs to be in here)}}
public static String rev(String str) {
String reversed = "hello there";
for (int i = 0; i < str.length(); i++) {
reversed = str.charAt(i) + reversed;
}
return reversed;
My question now is that what I need to put under the public static void main(String[] args) to make it work. Yes I understand that this is homework, I tried looking through the book for help(no luck). I tried looking on the internet(no luck thanks to my little understanding to the more advanced method). I would appreciate any help and thank anyone in advance.
I use netbeans if that would help any.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
读取 args 数组来执行此操作。
Read the args array to do so.
几件事:
由于您要将字符反向添加到字符串
reversed
中,因此它应该是一个空字符串开头:要从
main
调用此方法,请传递以下字符串:需要逆转。例如:或者,您可以读取要反转的字符串作为用户的输入,然后将其传递给
rev
函数。Few things:
Since you are adding the characters in reverse to the string
reversed
it should be an empty string to start with:To call this method from
main
pass it the string that needs to be reversed. Something like:Alternatively you can read the string to be reversed as an input from the user and then pass it to the
rev
function.在给定作业标签的情况下,尝试使其尽可能不透明,您至少应该重新检查
reversed
的初始值。按照你的方式,你最终会得到不同于想要的“ereht olleh”
的东西。至于您关于如何从命令行获取参数的具体问题,以下内容会打印出所有参数,并将为您自己的代码奠定良好的基础:
Trying to make this as opaque as possible given the homework tag, you should, at a minimum, re-examine your initial value of
reversed
. The way you have it, you will end up with something other than the desired"ereht olleh"
.As to your specific question on how to get the arguments from the command line, the following prints out all the arguments and would form a good basis for your own code:
读取控制台输入:
Read console input: