gpt4 book ai didi

java - java中if语句中的while循环

转载 作者:行者123 更新时间:2023-12-02 11:22:07 25 4
gpt4 key购买 nike

while 循环在 if 语句中不起作用,为什么它不起作用...以及 while 循环如何在 if 大括号中起作用。

public static void main(String[] args) {
int x = 30;

if (x < 20) {
System.out.print("This is if statement");
int a = 10;
while(a < 20) {
System.out.print("value of x : " + a );
a++;
System.out.print("\n");
}
} else {
System.out.print("This is else statement");
}
}

最佳答案

您将 x 声明为 30,if 循环执行 if(x<20)这总是错误的,所以它执行 else立即声明而不通过 if 语句。您要么必须声明x对于小于 20 的值或更改 if 语句。

关于java - java中if语句中的while循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49843755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com