【问题标题】:How to add values to empty string in for loop?如何在for循环中向空字符串添加值?
【发布时间】:2020-02-26 02:49:12
【问题描述】:

我在弄清楚如何将值附加到字符串末尾时遇到了一些麻烦。在给定的代码中,我们得到一个骰子值,并使用“*”打印它。例如,值 5 将在第一行有两个 *,在第二行有 1,在第三行有 2。为此,我创建了一个 for 循环,一旦达到该值就会终止并尝试附加一个空字符串。我不确定如何将其表示为每行中有一定数量星星的骰子。我尝试了其他方法,例如首先找到总值并创建第二个 while 循环来添加星星,但似乎无法弄清楚。

public String toString() {
    String stars = " ";

    for (int i = 0; i < value(); i++) {
        if (stars.contains("* * *")){
            //next line
        }
        stars += "*";
    }

    System.out.print(stars);
}

【问题讨论】:

    标签: java string loops printing contains


    【解决方案1】:

    没有必要那样做。你可以创建一个方法让它打印出来。如:

    static void draw(diceValue) {
        if (diceValue == 1) {
            for (int i = 0; i < diceValue; i++){
                System.out.println("*");
            }
        }
    }
    

    然后,在 main 方法中,您将调用此函数(方法)。另外,我在您的代码中没有看到 main 方法。

    public static void main(String[] args) {
        draw(1)
    }
    

    请注意,1 是输入参数或给定的骰子值。

    【讨论】:

    • 感谢您提供这种替代方法。我实际上需要返回一个字符串,所以我正在寻找一种将星号值添加到空字符串末尾的方法。
    • 你可以使用String.append
    【解决方案2】:

    如果你想打印骰子的值,这里有帮助你的程序

    public static void main(String[] args){
        int diceValue = 5;
        String stars = "";
        for(int i=1; i<= diceValue; i++) {
            if(diceValue % 2 != 0) {
                if( i==1 || i%2 == 0 ) {
                    stars += "* ";
                }else if(i==diceValue){
                    stars += "*";
                }else {
                    stars += "\n";
                    stars += " *\n";
                }
            }else {
                if( i%2 == 0 ) {
                    stars += "* \n";
                }else {
                    stars += "* ";
                }
            }
        }
        System.out.println(stars);
    }
    
    input: 
    DiceValue = 5 
    
    Output:
    * * 
     *
    * *
    
    input:
    DiceValue = 4
    
    Output:
    * * 
    * * 
    

    这是基于我对您希望以骰子格式打印输入的问题的理解。

    【讨论】:

      【解决方案3】:

      我尝试在不使用任何库的情况下解决同样的问题。我找到了一个有趣的方法来做到这一点。此代码有限制,它最多只能打印 6 个骰子值。我希望我奇怪的回答仍然有帮助。

       public static void main(String []args){
          int diceValue = 3;
          int[][] diceEquivalentValue = new int[][] {
                  {4,5,6}, {}, {2,3,4,5,6}, {6}, {1,3,5},
                  {6}, {2,3,4,5,6}, {}, {4,5,6}
          };
      
          String output = "";
          for( int x = 0; diceEquivalentValue.length > x ; x++ ) {
              output += contains(diceEquivalentValue[x], diceValue) ? "*" : " ";
              output += (x % 3) == 2 ? "\n" : "";
          }
      
          System.out.print( output );
       }
      
      private static boolean contains( int[] values, int diceValue ) {
          for ( int value : values ) {
              if ( value == diceValue ) {
                  return true;
              }
          }
          return false;
      }
      

      输出:

        *
       * 
      *  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-18
        • 2021-05-17
        • 2018-07-17
        • 2021-07-07
        • 1970-01-01
        相关资源
        最近更新 更多