对于循环无法正常工作(c ++)

For Loop not working properly (c++)

本文关键字:常工作 工作 于循环 循环      更新时间:2023-10-16

我一直在努力让这个程序正常工作。它确实可以编译,但是它不是提示用户输入,而是声明不正确的内容。

提示:

要求用户输入 10 个课程分数(从 0 到 100),然后(在标签中)说明有多少是通过分数。

我的代码:

#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
    int mark;
    int passinggrades = 0;
    for(int i = 0; i > 10; i++)
    {
        cout << "Enter Mark:";
        cin >> mark;
        while(mark >= 50)
        {
            passinggrades++;
        }
    }
    cout << j << " of your marks were passing grades.";
    return 0;
}

循环条件错误:

for (int i=0; i>10;i++)

反转比较符号

for (int i=0; i<10;i++)

另外,您可能希望if此行:

while(mark >= 50)

更改为

if(mark >= 50)

甚至输出线也是错误的。代码中没有j。这样做:

cout << passinggrades << " of your marks were passing grades."; 
for (int i=0; i>10;i++)

您从 i = 0 开始,并在i > 10时执行循环(从不,因为它为 0)。

我想你想要

    if(mark >= 50)

而不是无限循环

    while(mark >= 50)

另外,i<10就像其他人说的那样。

三个问题:一:当你说:

while(mark >= 50)
{
    passinggrades++;
}

50+ 的输入会让你的程序永远停留在外观上。而是使用:

if(mark >= 50)
{
    passinggrades++;
}

二:

不要在 for 循环中i>10,而是使用 i<10

三:(感谢ctor的评论)

j未定义,应替换为 passinggrades