如何在Java中向后打印多个字符串 [英] How to print several strings backwards in Java

查看:169
本文介绍了如何在Java中向后打印多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 字符串

    li>

  • 字符串向后和大写

  • 字符串长度



  • 还有更多的事情,但我甚至没有得到这一点,不想要求任何人为我完全写代码。一段时间后,我已经完成了它(我相信,除了一些地方)。

    绊倒我的那一块是倒退字。我们需要使用prinf将输出整齐地放在列中,但是如果我一次读取每个字符,我就无法做到这一点。所以我尝试设置一个 String backwardsWord =; 并添加每个字符



    这是绊倒我:

    pre $ code> for(int i = upperCaseWord.length() - 1; i> = 0; i-- )
    {
    backwardsWord + =(upperCaseWord.charAt(i)+);

    $ / code>

    我的问题是,当我打印它时,第一个单词正常工作。但是,之后的每个单词都被添加到前一个单词中。

    例如:如果我向后打印cat,dog和rat,它会显示


    TAC



    TACGOD

    TACGODTAR


    我显然希望阅读


    TAC





    TAR


    任何帮助,将不胜感激。

    解决方案

    它看起来像你的变量 backwardsWord 总是追加一个字符而不是在字之间重置。最简单的解决方法是通过将 backwardsWord 设置为空字符串来清除 backwardsWord

      backwardsWord =; //清除任何现有的字符backwardsWord 

    for(int i = upperCaseWord.length() - 1; i> = 0; i--)
    {
    backwardsWord + =(upperCaseWord.charAt(i)+);





    如果你正在建立一个字符串一个字符一次您将使用大量的内存,因为 Java字符串是不可变的。 / p>

    为了更有效地使用 StringBuilder 。这是为了建立像你在做什么字符。一旦你完成了,你可以使用 toString 方法来取出字符串。

      StringBuilder builder = new StringBuilder(); //创建存储字符的字符串生成器
    for(int i = upperCaseWord.length() - 1; i> = 0; i--)
    {
    builder.append( upperCaseWord.charAt(I)); //追加一个字符

    backwardsWord = builder.toString(); //将已完成的字符串存储到现有变量中

    每次重置backwardsword 。


    最后,因为你的目标是把字符串反过来,所以我们实际上可以在没有循环的情况下完成它。在这个答案中显示

      backwardsWord = new StringBuilder(upperCaseWord).reverse()。toString()

    这会创建一个新的 StringBuilder ,其中 upperCaseWord 中的字符反转字符,然后将最终字符串存储在 backwardsWord


    I am trying to take a file full of strings, read it, then print out a few things:

    • The string
    • The string backwards AND uppercase
    • The string length

    There are a few more things, however I haven't even gotten to that point and do not want to ask anyone to write the code entirely for me. After messing around with it for a while, I have it almost completed (I believe, save for a few areas).

    The piece that is tripping me up is the backwards word. We are required to put our output neatly into columns using prinf, but I cannot do this if I read each char at a time. So I tried setting a String backwardsWord = ""; and adding each character.

    This is the piece that is tripping me up:

    for(int i = upperCaseWord.length() - 1; i >= 0; i--)
    {
        backwardsWord += (upperCaseWord.charAt(i) + "");
    }   
    

    My issue is that when I print it, the first word works properly. However, each word after that is added to the previous word.

    For example: if I am printing cat, dog, and rat backwards, it shows

    TAC

    TACGOD

    TACGODTAR

    I obviously want it to read

    TAC

    GOD

    TAR

    Any help would be appreciated.

    解决方案

    It looks like your variable backwardsWord is always appending a character without being reset between words. The simplest fix is to clear the backwardsWord just before your loop by setting it to empty string.

    backwardsWord = ""; //Clear any existing characters from backwardsWord
    
    for(int i = upperCaseWord.length() - 1; i >= 0; i--)
    {
        backwardsWord += (upperCaseWord.charAt(i) + "");
    }
    


    If you are building up a String one character at a time you will be using a lot of memory because Java Strings are immutable.

    To do this more efficiently use a StringBuilder instead. This is made for building up characters like what you are doing. Once you have finished you can use the toString method to get the String out.

    StringBuilder builder = new StringBuilder(); //Creates the String builder for storing the characters
    for(int i = upperCaseWord.length() - 1; i >= 0; i--)
    {
        builder.append(upperCaseWord.charAt(i)); //Append the characters one at a time 
    }
    backwardsWord = builder.toString(); //Store the finished string in your existing variable
    

    This has the added benefit of resetting the backwardsWord each time.


    Finally, since your goal is to get the String in reverse we can actually do it without a loop at all as shown in this answer

    backwardsWord = new StringBuilder(upperCaseWord).reverse().toString()
    

    This creates a new StringBuilder with the characters from upperCaseWord, reverses the characters then stores the final string in backwardsWord

    这篇关于如何在Java中向后打印多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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