简体   繁体   English

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

[英]Java while loop print “+” based on the input of variable x

I want to be able to make a statement that prints (in this case "+") based upon the input of variable x.我希望能够根据变量 x 的输入做出打印(在本例中为“+”)的语句。 Example: If someone writes that x = 3, then it should print out "+++".示例:如果有人写 x = 3,那么它应该打印出“+++”。

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

if you want while loop and not for loop you may do this:如果你想要while循环而不是for循环,你可以这样做:

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

Explanation :解释 :
the x-- return the current value of x and only then decrease it by one so the code will be executed x times. x--返回 x 的当前值,然后将其减一,这样代码将被执行x次。

NOTE:笔记:
x-- overrides x value so if you want to keep the value simply use a temporary variable. x--覆盖x值,因此如果您想保留该值,只需使用临时变量即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM