【问题标题】:Java: Accumulate output pattern in string variable?Java:在字符串变量中累积输出模式?
【发布时间】:2013-05-20 20:08:01
【问题描述】:

可能有一个问题涵盖了这一点,但我在搜索中没有找到它。这个想法是在给定用户输入的字符和行数的情况下显示一个模式,如下所示:

x
xx
xxx
xxxx

xxxx
xxx
xx
x

但我需要使用 JOptionPane 来执行此操作。这是我所拥有的:

import javax.swing.JOptionPane;

public class loopPattern {

    public static void main(String[] args) {

        String in, num, output1 = null, output2 = "";
        int lines = 0;
        int i, n = 1;

        in = JOptionPane.showInputDialog("Please enter the character for the pattern:");
        num = JOptionPane.showInputDialog("Please enter the number of lines in the pattern:");
        lines = Integer.parseInt(num);

        for (i=1; i<=lines; i++) {

            for (n=1; n<=lines; n++) {

                output1 = (n +"\n");
            }
        }

        for (i=lines; i>=1; i--){

            for (n=lines; n>=1; n--){
                output2 = (n +"\n");
            }
        }

        JOptionPane.showMessageDialog(null, output1 +output2);
    }

}

我必须让它在每次用户点击“确定”时重复这种模式,并在他们点击“取消”时退出。我想我可以做到这一点,如果我能弄清楚字符串变量中的累积。非常感谢您的帮助。

【问题讨论】:

    标签: java string for-loop design-patterns joptionpane


    【解决方案1】:

    在字符串变量中累加称为StringBuilder。它允许您快速将内容附加到 StringBuilder 中,您可以从中调用 toString() 将其转换回字符串。

    StringBuilder sb = new StringBuilder();
    for (i=1; i<=lines; i++) {
        for (n=1; n<=lines; n++) {
            sb.append(n +"\n");
        }
    }
    

    如果您不能使用 StringBuilder,则使用 String 变量并使用“+”运算符将其自身的值分配给另一个字符串。这可以用 "+="

    简写
    String string = new String();
    string = string + "stuff to go on the string";
    // or as a shorthand equivalent
    string += "stuff to go on the string";
    
    /// loop example
    String myoutput = new String();
    for (i=1; i<=lines; i++) {
        for (n=1; n<=lines; n++) {
            myoutput += n +"\n";
        }
    }
    

    【讨论】:

    • 有没有办法可以解释不包括 StringBuilder?它不在这个练习的来源章节中,教授也没有在作业本身中提到它。我非常感谢任何帮助 - 教授采用“下沉或游泳”的方法,并拒绝回应有关一切(包括成绩)的询问。所以我有点卡住了。
    • 谢谢。这样可行。现在我只需要弄清楚我还做错了什么。 :)
    【解决方案2】:

    作为一种高级方法,您可以试试这个。创建两个 StringBuilder 实例。循环直到命中所需的lines。对于每次迭代,将X 附加到第一个StringBuilder 中,然后将StringBuilder 的全部内容附加到另一个(通过toString)中,\n 用于换行。在该循环完成后,为分隔符添加 2 个空行。然后,循环直到第一个StringBuilder 为空,删除每次迭代的最后一个字符(通过deleteCharAt(sb.length()-1))并再次通过toString 加上\n 将整个内容附加到另一个StringBuilder。完成后,第二个StringBuilder 应该具有您想要的模式。

    int lines = 4;
    StringBuilder sb = new StringBuilder();
    
    StringBuilder pattern = new StringBuilder();
    for(int i = 0; i < lines; i++){
      sb.append("X");
      pattern.append(sb.toString() + "\n");
    }
    pattern.append("\n\n");
    pattern.append(sb.toString() + "\n");
    while(sb.length() > 0){
      sb.deleteCharAt(sb.length() - 1);
      pattern.append(sb.toString() + "\n");
    }
    
    System.out.println(pattern.toString());
    

    【讨论】:

      【解决方案3】:

      如果使用 StringBuilder 太高级,你可以简单地使用字符串来获得相同的效果:

      String output1 = "";
      for (i=1; i<=lines; i++) {
          for (n=1; n<=lines; n++) {
              output1 = output1.concat(n +"\n");
              // note the below commented out code should also work:
              //output1 = output1 + n + "\n";
          }
      }
      

      这比使用 StringBuilder 效率低得多,因为内部循环的每次迭代都会创建一个新字符串并将其分配给 output1。

      【讨论】:

      • 我确信它的效率较低,但我还没有使用 StringBuilder。这个 output1 = output1 + n + "\n";不工作,但。如果有意义的话,我需要输出 1 位于 n 或 in*n 处。所以我现在必须弄清楚。
      • 看起来像 @user1676075 通过将新的行连接移动到外循环找到了 solution。 'A += B' 只是 'A = A + B' 的简写
      【解决方案4】:

      你的循环应该看起来更像:

      for (i=1; i<=lines; i++) {
      
                  for (n=0; n<i; n++) {
      
                      output1 += in; 
                  }
             output += "\n";
              }
      

      假设您不能使用 StringBuilder(根据其他帖子,这是一个更好的选择)。

      【讨论】:

        猜你喜欢
        • 2017-11-05
        • 1970-01-01
        • 2015-10-31
        • 1970-01-01
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        • 2021-06-15
        • 2018-01-21
        相关资源
        最近更新 更多