循环逐步添加字符串填充的 for 循环?

huangapple go评论72阅读模式
英文:

For loop for progressively adding string padding?

问题

所以我对Java还不太熟悉,如果这个问题不好请原谅。

我尝试使用for循环来根据用户输入的值(在这种情况下为8)连续添加填充到一个字符串(在本例中为“*”)。因此,当输入为8时,会得到以下结果:

    *  --> 没有填充
     * --> printf("%2s", "*");
      * --> printf("%3s", "*");
       * --> 等等。
        *
         *
          *
           * --> 最后会以输入值减1的偏移结束,所以在这个例子中是7。

除了逐个编写每个语句外,我不知道如何做到这一点,但这样就不是使用for循环了。这需要手动更改格式化的字符串,对吗?不能自动完成吗?
英文:

So im pretty new to java, sorry if this is a bad question.

Im trying to use a for loop to continuously add padding to a string (in this case “*”), dependent on a user inputted value. So an 8 would give me:

*  --> no padding
 * --> printf("%2s", "*");
  * --> printf("%3s", "*");
   * --> etc.
    *
     *
      *
       * --> ending with the input -1 offset, so 7 in this case.

I don't know how to do this other than writing each statement out, but then its not using a for loop.This requires manual changing of the formatted string right?, not something that can be automatic?.

答案1

得分: 1

可以按以下方式动态创建格式

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("您想要多少填充:");
        int padding = Integer.parseInt(scanner.nextLine());

        System.out.printf("%" + padding + "s%n", "*");

        // 使用此技巧的模式
        for (int i = 1; i <= 10; i++) {
            System.out.printf("%" + i + "s%n", "*");
        }
    }
}

**一个示例运行**

您想要多少填充8
       *
      *
     *
    *
   *
  *
 *
*
*
 *
  *
   *
    *
     *
      *
       *
英文:

You can create the format dynamically as shown below:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print(&quot;How much padding do you want: &quot;);
		int padding = Integer.parseInt(scanner.nextLine());

		System.out.printf(&quot;%&quot; + padding + &quot;s%n&quot;, &quot;*&quot;);

		// A pattern using this trick
		for (int i = 1; i &lt;= 10; i++) {
			System.out.printf(&quot;%&quot; + i + &quot;s%n&quot;, &quot;*&quot;);
		}
	}
}

A sample run:

How much padding do you want: 8
       *
*
 *
  *
   *
    *
     *
      *
       *
        *
         *

答案2

得分: 0

public class Main{
    public static void main(String[] args) {
        for (int i=0; i<10; i++) {
            IntStream.range(0, i).forEach((c)->System.out.print("   "));
            System.out.println("*");
        }
    }
}
英文:

You could use:

public class Main{
    public static void main(String[] args) {
        for (int i=0; i&lt;10; i++) {
            IntStream.range(0, i).forEach((c)-&gt;System.out.print(&quot; &quot;));
            System.out.println(&quot;*&quot;);
        }
    }
}

Per each i, you are printing the i times empty strings padding, postfixed by *.

Change the characters (&quot; &quot; or *) with whatever you wish.

答案3

得分: 0

"The "no padding" can be done using printf(&quot;%1s&quot;, &quot;*&quot;).

Now look at the first parameter to printf in all the calls. It's just a string, with an incrementing number from 1 to 8. So build that string using string-concatenation:

printf(&quot;%&quot; + i + &quot;s&quot;, &quot;*&quot;)  // if i is 1 to 8
printf(&quot;%&quot; + (i + 1) + &quot;s&quot;, &quot;*&quot;)  // if i is 0 to 7
英文:

The "no padding" can be done using printf(&quot;%1s&quot;, &quot;*&quot;).

Now look at the first parameter to printf in all the calls. It's just a string, with an incrementing number from 1 to 8. So build that string using string-concatenation:

printf(&quot;%&quot; + i + &quot;s&quot;, &quot;*&quot;)  // if i is 1 to 8
printf(&quot;%&quot; + (i + 1) + &quot;s&quot;, &quot;*&quot;)  // if i is 0 to 7

答案4

得分: 0

请尝试这个。

for (int i = 0; i < 8; ++i)
    System.out.println(" ".repeat(i) + "*");

输出结果:

*
 *
  *
   *
    *
     *
      *
       *
英文:

Try this.

for (int i = 0; i &lt; 8; ++i)
    System.out.println(&quot; &quot;.repeat(i) + &quot;*&quot;);

output

*
 *
  *
   *
    *
     *
      *
       *

huangapple
  • 本文由 发表于 2020年10月5日 03:09:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64198782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定