我在C 初学者中发现了无限的循环问题
I got infinite loop issues in c++ beginner
使用编程很新,想知道您是否可以提供帮助。用我的代码获得了无限循环
int main()
{
int age, finalMark;
cout << "enter age: ";
cin >> age;
cout << "enter mark: ";
cin >> finalMark;
while (age != 0)
{
if(age < 30 && finalMark > 65)
cout << "You are the an ideal candidate" << endl;
else
cout << "You are not the ideal candidate. Goodbye" << endl;
}
return 0;
}
任何帮助都会很感激,很抱歉,如果它非常基本/易于解决
使用循环时,请确保某个时候条件不正确,否则,您最终会出现无尽的环路。
在这里,如果年龄的价值最初与0不同,那么您将永远不会脱离循环,因为您不会在循环中任何地方更改它。
while (age != 0)
{
if(age < 30 && finalMark > 65)
cout << "You are the an ideal candidate" << endl;
else
cout << "You are not the ideal candidate. Goodbye" << endl;
}
如果您想简单地检查条件,并且只能根据其结果进行操作,请使用" If"语句:
if (age != 0)
{
if(age < 30 && finalMark > 65)
cout << "You are the an ideal candidate" << endl;
else
cout << "You are not the ideal candidate. Goodbye" << endl;
}
相关文章:
- 过载'operator new'如何导致无限循环?
- C++正则表达式无限循环
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 遍历链表时的无限循环
- 循环链表:无限循环
- 比较两个字符串后卡在无限循环中
- 在做一段时间内检查字符的无限循环
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 可能的无限循环
- 超出 int 的值范围后的无限循环
- 为什么这段代码会导致无限循环?
- 运行无限循环的最小二叉树问题
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 连接线程时的无限循环
- 为什么这种方法会进入无限循环?
- 由于QTextStream的无限循环
- Qt 在另一个线程中无限循环
- 从文件 C++ 读取时的无限循环
- 试图找出为什么我的代码保持无限循环