Java while-做代码困难

Java while- do code difficulties

这是什么问题,

public class Whileloop1 {
    public static void main(String[] args) {
    int x = 0;
    if (x < 10) {
    System.out.print("x is less than 10!");
}
x++ ;
 }
}

我希望程序输出字符串多次 (10) 直到 x 达到 10,然后停止打印字符串。所以总而言之,我想要字符串打印 10 次。

谁能回答这个问题,可以将代码复制到他们的答案中并修复吗?谢谢!

你的 x++ 需要放在花括号中并使用 while 循环

int x = 0;
while(x < 10) {
System.out.print("x is less than 10!");
 x++ ;
}

下面是解决循环 10 次问题的部分方法。

int x = 0;
while (x < 10)
{
   //do something

   //Increment x by 1 [equivalent to: (x = x + 1)]
   x++;
}

或者如果您知道循环需要固定数量的迭代,您可以像这样使用 for 循环。

for (int i = 0; i < 10; i++) 
{
   //do something
}