我在代码中遇到了while循环问题。说明指示"将循环控制变量设置为第一次自动进入循环的值"。有关如何做到这一点的任何建议?任何输入将不胜感激。谢谢!
} //结束主要
答案 0 :(得分:0)
我会假设您不知道这些术语的大部分意思。
while循环采用以下语法(我将在C#中执行此操作,因为我不确定您正在编写哪种语言;更新原始帖子并且我将更改我的样品):
while (/* loop control/condition here */)
{
// Code here
}
这个用英语(伪代码)评估如下:
while the loop control is true
do this
when it equals false leave the while
所以你要做的是初始化一个变量(为了清楚起见,我在这里使用类型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 */);
伪代码:
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.