【问题标题】:In Java, what is the simplest way to reverse the string output of this program? [closed]在Java中,反转该程序的字符串输出的最简单方法是什么? [关闭]
【发布时间】:2017-09-23 19:09:57
【问题描述】:

我正在尝试做的是从右到左逐步“反向打印”一个字母字符串,如下所示:

A
BA
CBA
DCBA
etc...

这是我目前以正常顺序打印的基础:

public static void main(String[] args) 
{
    String output = "";
    for(char alphabet = 'A'; alphabet <='Z'; alphabet++ )
        {
            output += alphabet;
            System.out.println(output);
        }
}

任何帮助都将不胜感激。

【问题讨论】:

  • "Sorry if this is a bad question but I'm a noob who's been searching for an answer to this problem for like an hour now." -- 不要发布此类信息,因为它无助于我们更好地了解您的问题或您的努力。相反,更好的是显示您在搜索中找到的具体结果以及您为解决此问题所做的最大诚意尝试。否则,您的问题将被视为低质量问题,可能无法回答,并且有被关闭的风险。
  • output += alphabet(有点)表示output = output + alphabet。知道了这一点,你能明白如何让它做你想做的事吗?
  • @AndyTurner 我假设我需要一些东西来打印当前字母左侧的加法,但我不知道如何准确地“用词”。 “输出+=字母表(某种)意味着输出=输出+字母表”是什么意思。对了?
  • @minusxp per the spec,其实就是output = (String) (output + alphabet)。它有额外的演员阵容在这里是一个很大程度上无关紧要的细节,但这就是它实际上相同。

标签: java arrays string netbeans


【解决方案1】:

output += alphabet; 更改为output = alphabet + output; 这样就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-16
    • 2016-05-10
    • 2010-10-25
    • 2019-12-27
    • 2012-06-01
    • 2014-07-01
    • 2010-09-16
    • 2021-06-25
    相关资源
    最近更新 更多