Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 自动进入while循环_Loops_While Loop - Fatal编程技术网

Loops 自动进入while循环

Loops 自动进入while循环,loops,while-loop,Loops,While Loop,我的代码中的while循环有问题。指令说明将循环控制变量设置为第一次自动进入循环的值。有没有关于我如何做到这一点的建议?如有任何意见,将不胜感激。谢谢大家! }//end main我假设您不知道这些术语的大部分含义 while循环采用以下语法,我将用C语言编写,因为我不确定您使用的是哪种语言;更新您的原始帖子,我将更改我的示例: while (/* loop control/condition here */) { // Code here } 这在英文伪代码中的计算结果如下: whi

我的代码中的while循环有问题。指令说明将循环控制变量设置为第一次自动进入循环的值。有没有关于我如何做到这一点的建议?如有任何意见,将不胜感激。谢谢大家!


}//end main

我假设您不知道这些术语的大部分含义

while循环采用以下语法,我将用C语言编写,因为我不确定您使用的是哪种语言;更新您的原始帖子,我将更改我的示例:

while (/* loop control/condition here */)
{
    // Code here
}
这在英文伪代码中的计算结果如下:

while the loop control is true
    do this
when it equals false leave the while
do this code
keep repeating code until the condition is false
所以你要做的是初始化一个变量,为了清楚起见,我在这里使用bool类型,并把它放在括号之间作为循环控制

以下是一个小样本:

bool daytime = true; // This is the loop control.
int i = 0;

while (daytime == true) // We are seeing if it's day out.
{
     System.out.println("It is daytime.");
     if (i == 6) {daytime = false;} // If our counter hits 6:00 it's nighttime.
     i = i + 1; // Increment our counter.
}
无论如何,我希望这能帮助你。祝你好运

编辑:边做边做 我可能有点误解了您最初的问题,do while循环就是这样的:

语法:

do
{
    // Code here
} while (/* loop control/condition here */);
伪代码:

while the loop control is true
    do this
when it equals false leave the while
do this code
keep repeating code until the condition is false
样本:

bool daytime = true; // This is the loop control.
int i = 0;

do
{
     System.out.println("It is daytime.");
     if (i == 6) {daytime = false;} // If our counter hits 6:00 it's nighttime.
     i = i + 1; // Increment our counter.
} while (daytime == true) // We are seeing if it's day out.

你能分享你的代码吗?你可以使用do{…}whilecondition。。或者它必须是一个whilecondition{…}?实际上我必须同时使用这两种语言。我怎样才能把它翻译成Java语言?对不起,我忘了指定那个。顺便说一下,谢谢你的回答@求你了,编程员1完成了!这两种语言如此相似,我只需更换控制台。谢谢我只是不明白如何让它自动进入while循环。我可以使用什么控制变量?@Beg.Programmer1 while循环第一次出现时最初为真的任何变量。除非我误解了你。如果您谈论的是进入循环,不管条件是否为真,那么您将看到一个do while循环。如果这是你想要的,我可以更新答案@我为你加了一段时间。如果这是你想要的,请记住接受我的回答!祝你好运