如何完成反向字符串?

发布于 2024-09-27 23:55:50 字数 669 浏览 4 评论 0原文

所以我的老师告诉我制作一个字符串,使我输入的任何内容都以相反的顺序排列 (例如“你好”变成“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅气尐潴 2024-10-04 23:55:50
public static String rev(String str) {
  String reversed = "";
  for (int i = 0; i < str.length(); i++) {
      reversed = str.charAt(i) + reversed;
  }
  return reversed;
}

public static void main(String[] args) {
  if (args.length > 0) {
    System.out.println(rev(args[0]));
  }
}

读取 args 数组来执行此操作。

public static String rev(String str) {
  String reversed = "";
  for (int i = 0; i < str.length(); i++) {
      reversed = str.charAt(i) + reversed;
  }
  return reversed;
}

public static void main(String[] args) {
  if (args.length > 0) {
    System.out.println(rev(args[0]));
  }
}

Read the args array to do so.

十秒萌定你 2024-10-04 23:55:50

几件事:

由于您要将字符反向添加到字符串 reversed 中,因此它应该是一个空字符串开头:

String reversed = "";

要从 main 调用此方法,请传递以下字符串:需要逆转。例如:

public static void main(String...args[]) {
  System.out.println(rev("hello there"));
}

或者,您可以读取要反转的字符串作为用户的输入,然后将其传递给 rev 函数。

Few things:

Since you are adding the characters in reverse to the string reversed it should be an empty string to start with:

String reversed = "";

To call this method from main pass it the string that needs to be reversed. Something like:

public static void main(String...args[]) {
  System.out.println(rev("hello there"));
}

Alternatively you can read the string to be reversed as an input from the user and then pass it to the rev function.

忘年祭陌 2024-10-04 23:55:50

在给定作业标签的情况下,尝试使其尽可能不透明,您至少应该重新检查 reversed 的初始值。按照你的方式,你最终会得到不同于想要的“ereht olleh”的东西。

至于您关于如何从命令行获取参数的具体问题,以下内容会打印出所有参数,并将为您自己的代码奠定良好的基础:

public static void main (String[] args) {
    for (int i = 0; i < args.length; i++)
        System.out.println (args[i]);
    }
}

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:

public static void main (String[] args) {
    for (int i = 0; i < args.length; i++)
        System.out.println (args[i]);
    }
}
我乃一代侩神 2024-10-04 23:55:50

读取控制台输入:

public static void main(String[] args) {
  String curLine = ""; // Line read from standard in
  System.out.println("Enter a line of text");
  InputStreamReader converter = new InputStreamReader(System.in);
  BufferedReader in = new BufferedReader(converter);
  curLine = in.readLine();

  System.out.println(rev(curLine));
}

Read console input:

public static void main(String[] args) {
  String curLine = ""; // Line read from standard in
  System.out.println("Enter a line of text");
  InputStreamReader converter = new InputStreamReader(System.in);
  BufferedReader in = new BufferedReader(converter);
  curLine = in.readLine();

  System.out.println(rev(curLine));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文