Java while循环根据变量x的输入打印“+”

Java while loop print "+" based on the input of variable x

我希望能够根据变量 x 的输入做出打印(在本例中为“+”)的语句。示例:如果有人写 x = 3,那么它应该打印出“+++”。

public static void runLoop(int x){
    
    while(//Code//){
        System.out.print("+");
    }
}

如果你想要 while 循环而不是 for 循环你可以这样做:

public static void runLoop(int x){
    
    while(x--!=0){
        System.out.print("+");
    }
}

解释:
x-- return x 的当前值,然后将其减一,这样代码将被执行 x 次。

注意:
x-- 覆盖 x 值,因此如果您想保留该值,只需使用一个临时变量即可。