英文:
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("How much padding do you want: ");
		int padding = Integer.parseInt(scanner.nextLine());
		System.out.printf("%" + padding + "s%n", "*");
		// A pattern using this trick
		for (int i = 1; i <= 10; i++) {
			System.out.printf("%" + i + "s%n", "*");
		}
	}
}
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<10; i++) {
            IntStream.range(0, i).forEach((c)->System.out.print(" "));
            System.out.println("*");
        }
    }
}
Per each i, you are printing the i times empty strings padding, postfixed by *.
Change the characters (" " or *) with whatever you wish.
答案3
得分: 0
"The "no padding" can be done using printf("%1s", "*").
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("%" + i + "s", "*")  // if i is 1 to 8
printf("%" + (i + 1) + "s", "*")  // if i is 0 to 7
英文:
The "no padding" can be done using printf("%1s", "*").
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("%" + i + "s", "*")  // if i is 1 to 8
printf("%" + (i + 1) + "s", "*")  // 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 < 8; ++i)
    System.out.println(" ".repeat(i) + "*");
output
*
 *
  *
   *
    *
     *
      *
       *
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论