所以我对 Python 和一般编程很陌生。我的问题是关于我在这里的 while 循环标志。这有效,但不像我认为的那样。我想我不理解某些东西,所以如果有人能够解释,那就太好了。
根据我的理解,只要满足我的一个条件,就应该立即跳出循环。因此,如果我输入“q”,它应该会中断并停止循环。但发生的情况是它不断循环,然后爆发。所以它通过最后一个提示并打印异常。
(Python 版本为 3.8.5)
# Statement that tells the user what we need.
print("Enter two numbers and I will tell you the sum of the numbers.")
# Lets the user know they can press 'q' to exit the program.
print("Press 'q' at anytime to exit.")
keep_going = True
# Loop to make the program keep going until its told to stop.
while keep_going:
# Prompt for user to input first number and store it in …Run Code Online (Sandbox Code Playgroud) 该程序的目标是掷两个骰子,直到它们都等于 6。当我在 while 语句中使用 && 时,它在一个骰子等于 6 后停止。这是为什么呢?不应该&& 测试第一个条件,然后测试第二个条件是否正确?
while ((diceOne != 6) || (diceTwo != 6)) {
diceOne = numberGeneration.Next(1, 7);
diceTwo = numberGeneration.Next(1, 7);
attempt = ++attempt;
Console.WriteLine("Your first dice is: " + diceOne + " your second dice is: " + diceTwo);
Console.WriteLine("Press any button to continue");
Console.ReadKey();
}
Console.WriteLine("It took you " + attempt);
Console.WriteLine("Press any key to continue");
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud) for()或while() - 哪个最好?
for (i=1; i<a; i++)
/* do something */
Run Code Online (Sandbox Code Playgroud)
要么
i=1;
while (i<a) {
/* do something */
i++;
}
Run Code Online (Sandbox Code Playgroud) 我有一个关于在C++中使用goto语句的问题.我理解这个话题是有争议的,并且对任何广泛的建议或论点都不感兴趣(我通常偏离使用goto).相反,我有一个特定的情况,并想要了解我的解决方案,它使用goto语句,是否是一个好的.我不会称自己是C++的新手,但也不会将自己归类为专业级程序员.生成我的问题的代码部分一旦启动就会在无限循环中旋转.伪代码中线程的一般流程如下:
void ControlLoop::main_loop()
{
InitializeAndCheckHardware(pHardware) //pHardware is a pointer given from outside
//The main loop
while (m_bIsRunning)
{
simulated_time += time_increment; //this will probably be += 0.001 seconds
ReadSensorData();
if (data_is_bad) {
m_bIsRunning = false;
goto loop_end;
}
ApplyFilterToData();
ComputeControllerOutput();
SendOutputToHardware();
ProcessPendingEvents();
while ( GetWallClockTime() < simulated_time ) {}
if ( end_condition_is_satisified ) m_bIsRunning = false;
}
loop_end:
DeInitializeHardware(pHardware);
}
Run Code Online (Sandbox Code Playgroud)
pHardware指针从ControlLoop对象外部传入并具有多态类型,因此对我来说使用RAII并在main_loop中创建和破坏硬件接口本身没有多大意义.我想我可以让pHardware创建一个临时对象,代表硬件的"会话"或"使用",可以在main_loop退出时自动清理,但我不确定这个想法是否会让某些人更清楚否则我的意图是什么.循环中只有三种方式:第一种方法是从外部硬件读取坏数据; 第二个是如果ProcessPendingEvents()指示用户启动的中止,这只会导致m_bIsRunning变为false; 最后一个是如果在循环的底部满足结束条件.我还应该注意,main_loop可以在ControlLoop对象的生命周期内多次启动和完成,因此它应该在m_bIsRunning = false之后干净利落地退出.
另外,我意识到我可以在这里使用break关键字,但是main_loop中的大多数伪代码函数调用并没有真正封装为函数,只是因为它们需要有很多参数,或者它们都需要访问成员变量.在我看来,这两种情况都比让简单地将main_loop作为一个更长的函数更容易混淆,而且由于big while循环的长度,一个类似的声明goto loop_end似乎对我来说更清晰.
现在提出一个问题:如果您在自己的代码中编写它,这个解决方案会让您感到不舒服吗?它对我来说确实有些不对劲,但之后我从未在C++代码中使用过goto语句 - 因此我请求专家提供帮助.我还缺少哪些其他基本想法可以使这些代码更清晰?
谢谢.
当我将Visual Studio 2010配置从Debug更改为Release时,我得到一个非常奇怪的行为:
我有一个BackgroundWorker:_bg,DoWork我有:
iswaiting = true;
_bg.ReportProgress(1, filePath);
while (iswaiting)
{
;
}
//My other part of code (EDIT: something do to with the `result` I get from the user.)
Run Code Online (Sandbox Code Playgroud)
在ProgressChanged我有一个MessageBox和用户交互后,iswaiting将设置回false并且_bg DoWork程序将继续.
void _bg_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//my other part of code........
result = Microsoft.Windows.Controls.MessageBox.Show("Question" ,"Title", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning);
iswaiting=false;
log(iswaiting.toString());
}
Run Code Online (Sandbox Code Playgroud)
所有这些作品非常好,当我从Visual Studio运行或建造中调试模式,但是当我建立它松开,我从来没有走出的while(iswaiting)循环,但我可以看到该日志iswaiting已被设置回false.
编辑 …
在while循环中有几个关于switch语句的帖子,除了它们都没有在C语言中完成的事实,至少从我看到的情况来看.C++可以创建布尔表达式,我知道,但不是在C中.我有一个包含开关控件的while循环.但是,当我在switch中编写break语句时,它会回到循环的开头并使我的程序永远运行.忽略我使用的功能,因为它们肯定会起作用.我只需要澄清一下我对嵌套的处理方法.谢谢!
Here is my main.c:
while(1)
{
printf("0) Exit\n1) List Tasks\n2) Add Task\n");
printf("3)Delete Task\n4) Add Task From File\n");
printf("What would you like to do?\n");
fgets(buf1, 50, stdin);
p = atoi(buf1);
switch(p)
{
case 0:
break;
case 1:
printTaskList(pTaskList);
break;
case 2:
printf("Enter task name: ");
fgets(buf2,100,stdin);
printf("Enter task priority: ");
fgets(buf3,100,stdin);
printf("Enter task start date: ");
fgets(buf4,50,stdin);
pTask = makeTask(buf2,buf4,buf3);
addTaskToEnd(pTaskList,pTask);
break;
case 3:
printTaskList(pTaskList);
printf("What task would you like to delete? ");
fgets(buf6,50,stdin);
taskNum = atoi(buf6);
removeTask(pTaskList,taskNum);
break; …Run Code Online (Sandbox Code Playgroud) 我从未想过这是可能的,但阅读了一些相互矛盾的评论,并认为我会问专家.
如果我正在逐行读取文件的while循环,如果当前迭代将是循环中的最后一次迭代,是否有办法执行某些代码?我知道我可以简单地将这段代码放在while循环之后,这样代码就会在最后一行之后执行,但我只是想知道迭代是否有任何方法来检测它的位置.
谢谢!
Greetings Stack Overflow用户,今晚我来找你,帮助我创建一个Java程序.我对Java比较新,所以请原谅我对这个主题的无知.我制作了一个Java程序,它是一个"Rock""Paper""Scissors"游戏,其中一个语句中似乎有错误.
import java.util.Scanner;
public class TheAntlers {
public static void main(String[] args) {
int playerHumanWins = 0;
int playerComputerWins = 0;
int numberOfTies = 0;
int computerResult;
Scanner input = new Scanner(System.in);
while(true) {
String startGame;
String playerHuman;
String playerComputer = " ";
System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): ");
startGame = input.nextLine();
startGame = startGame.toUpperCase();
if(startGame.equals("N")) {
System.out.println("NO!");
break;
}
else if(! startGame.equals("Y")) {
startGame = startGame.toLowerCase();
System.out.println("Sorry, " + startGame + " is …Run Code Online (Sandbox Code Playgroud) 我刚开始在学校学习Java,现在我被卡住了.
public static void main(String[] args) {
int count = 0;
int point = 5;
while(point != count++){
System.out.println(count);
}
Run Code Online (Sandbox Code Playgroud)
控制台:1 2 3 4 5
为什么数字"5"仍然打印?是不是这个while循环应该只在点!= count + 1时运行?然后它应该在5 = 5时停止,不是它(并且不会打印"5")?
非常感谢.
我正在Do While loop我的项目中工作,第一次工作正常.
之前while statement,我为一个数组赋值,我能够在代码底部成功打印数组,但是当我在循环顶部检查时它变为0.
码:
$looparray = array();
$loopend = 0;
$arraymer = array();
$poolafirtsid = $previous_array_values; //previous array values
do {
if (sizeof($looparray) == 0) {
$firstsponarray = $poolafirtsid;
} else {
$firstsponarray = $looparray;
}
$firstsponarray = getUserArray($poolafirtsid);
//get user arraylist of first
foreach ($firstsponarray as $avalue) {
$rooparray = membercount($avalue);
$bsponarray = getUserArray($avalue);
//get second users arraylist 9
if (sizeof($bsponarray > 0)) …Run Code Online (Sandbox Code Playgroud)