如何增加一个数字,停止它,在C++上使用 if/else 语句
How do I increase a number, stop it, use an if/else statement on C++?
我是这个社区的新手。所以详细说明我的问题:我想让一个数字从 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)
说定义和初始化i
以0
,在x <= 10
为真时循环,并在每次迭代后将x
增加2
。它基本上与此相同:
{
int x = 0;
while (x <= 10)
{
// Your code inside the loop
x += 2;
}
}
所以你循环将有以下值x
:0
、2
、4
、6
、8
、10
,然后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;
}
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- if数组上的随机数
- 将按位if条件转换为普通if条件
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 使用if-else将数字转换为单词
- 为什么简单的算术减法在"if"条件下不起作用?
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句