如何增加一个数字,停止它,在C++上使用 if/else 语句

How do I increase a number, stop it, use an if/else statement on C++?

本文关键字:C++ if 语句 else 增加 何增加 数字 一个      更新时间:2023-10-16

我是这个社区的新手。所以详细说明我的问题:我想让一个数字从 0 开始,计数到 10 (1,2,3,4,5,6,7,8,9,10),然后做一个 if 语句说如果 x <= 10 打印"hello world"。后面跟着一个 else 语句:else 打印"你好黑暗,我的老朋友"。

这是我输入的代码:

#include <iostream> 
using namespace std;
int main() {
for (int x = 0; x <= 10; x += 2) {
    cout << x << endl;
    if (x = 20) {
        cout << "hello world" << endl;
    }
    else {
        cout << "hello darkness my old friend" << endl;
    }
}
return 0;
}

问题是每次我运行它时,它总是说:

0
hello world

我想说的是:

0
2
4
6
8
10
hello world

即使我改变:

if (x = 10)

自:

if (x = *any number higher than 10*)

它仍然具有相同的输出始终:

0
hello world

请帮我解决这个问题。

编辑:我看到每个人都对我想要的输出感到困惑。我想要的输出是,如果陈述为真,打印

0
2
4
6
8
10
hello world

否则,打印:

hello darkness my old friend
#include <iostream> 
using namespace std;
int main() {
    for (int x = 0; x <= 20; x += 2) 
    {
        cout << x << endl;
        if (x == 10) 
        {
            cout << "hello world" << endl;
        }
        else if(x > 10)
        {
            cout << "hello darkness my old friend" << endl;
        }
    }
    return 0;
}

我认为这就是你想要的;我将循环限制增加到 20,修复了与 if(x ==10) 的比较问题,我添加了 否则if(x > 10)处理大于 10 的数字。希望对您有所帮助!

我想

这就是你要找的:-

#include <iostream>
using namespace std;
int main() {
    int num;
    cin>>num;
    //num taken from user
    if(num%2==0) {
        for (int x = 0; x <= num; x += 2) {
            cout << x << endl;
        }
        cout<<"Hello World" <<endl;
    } else {
        cout << "hello darkness my old friend" << endl;
    }
    return 0;
}

输出将像:-

let num=12;   //entered by USER 
0
2
4
6
8
10
12
Hello World
// In Case of False Condition
hello darkness my old friend  

除了赋值/比较问题之外,你的循环永远不会达到比10更高的数字,因为那是循环条件说循环将结束的时候。

该语句for (int x = 0; x <= 10; x += 2)说定义和初始化i0,在x <= 10为真时循环,并在每次迭代后将x增加2。它基本上与此相同:

{
    int x = 0;
    while (x <= 10)
    {
        // Your code inside the loop
        x += 2;
    }
}

所以你循环将有以下值x0246810,然后x <= 10为假,循环将结束。


回到作业/比较问题,这就是有些人喜欢写的原因,例如 20 == x而不是x == 20.如果错误地使用赋值而不是比较,则表达式将被20 = x,这不是有效的表达式,编译器将生成错误。

如今,大多数编译器在用作条件时都能够检测到赋值x = 20,并可以为此生成警告。您可能希望启用更高的警告级别,以便编译器告知您有关它的信息。

问题是您将 10 分配给 x,因此 (x=10) == true

我试图修复它,我至少把它数到 10。这是我的代码:

#include <iostream>
using namespace std;
int main() {
for (int x = 0; x <= 10; x += 2) {
    cout << x << endl;
    if (x == 20) {
        cout << "hello world" << endl;
    }
    else {
        cout << "hello darkness my old friend" << endl;
    }
}
return 0;
}

下面是输出:

0
hello darkness my old friend
2
hello darkness my old friend
4
hello darkness my old friend
6
hello darkness my old friend
8
hello darkness my old friend
10
hello darkness my old friend

这是您需要的结果吗?

试试这个

#include <iostream> 
using namespace std;
int main() 
{
    for (int x = 0; x <= 10; x += 2) 
    {
        cout << x << endl;
        if (x == 10)
            cout << "hello world" << endl;
        else 
            cout << "hello darkness my old friend" << endl;
    }
    return 0;
}