为什么我的 if/else 语句没有产生预期的结果?
Why isn't my if/else statement producing the desired results?
我的程序有问题。我实际上已经完成了它,但最后并没有正常工作。我用两种方式编写了它,一种是使用 switch 语句,另一种是使用 if else 语句,但两者都无法正常工作。它们都为我编译并具有干净的构建,但不会按预期运行。
#include <iostream>
#include <cstdio>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int i = 0;
const int size = 27;
char newline = 'n';
char *pnumber = 0;
int selection = 0;
cout << "PRINGING CONTENTS OF ARRAY" << endl;
cout << "====================================================" << endl;
char alphabet[size] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'
, 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'
, 'Z' , 0 };
for (i = 0; i < size; i++)
{
cout << alphabet[i] << " ";
}
cout << newline;
cout << newline;
cout << "This is the title to your Program related to the alphabet."
<< endl;
cout << endl;
cout << "Select the index that coincides with the alphabet." << endl;
cout << "For example, the number 7 should display the letter G" << endl;
cout << endl;
cout << "Enter an index between 1 and 26: ";
cin >> i;
cout << "The number you selected: " << i;
cout << newline;
cout << "The letter at this index: " << alphabet[i - 1];
cout << endl;
cout << newline;
cout << newline;
cout << "PRINTING CONTENTS OF ARRAY and adding x to every other element" << endl;
pnumber = &alphabet[1];
for (i = 0; i < size; i += 1)
{
if (i % 2 == 0)
{
cout << alphabet[i] << " ";
}
else
{
cout << "x ";
}
}
cout << newline;
cout << newline;
cout << "PRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl;
cout << "=========================================================" << endl;
cout << "Do you want the letters of the alphabet starting from index 0, A, or index 1, B: ";
cin >> selection;
switch (selection)
{
case 0:
{
for (i = 0; i < size; i += 2)
{
cout << "Even Numbered Elements=" << i << " ";
cout << "Contents of Element within Array is=" << alphabet[i];
break;
}
}
case 1:
{
for (i = 1; i < size; i += 2)
{
cout << "Odd Numbered Elements=" << i << " ";
cout << "Contents of Element within Array is=" << alphabet[i] << endl;
break;
}
}
default: cout << "You entered an invalid esponse.";
}
cout << endl;
return 0;
} //End of Int Main
如果其他的话,最后一部分是这样的:
cout << newline;
cout << newline;
cout << "PRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl;
cout << "=========================================================" << endl;
cout << "Do you want the letters of the alphabet starting from index 0, A, or index 1, B: ";
cin >> selection;
if (selection = 0){
for (i = 0; i < size; i += 2)
{
cout << "Even Numbered Elements=" << i << " ";
cout << "Contents of Element within Array is=" << alphabet[i] << endl;
}
}
else{
for (i = 1; i < size; i += 2)
{
cout << "Odd Numbered Elements=" << i << " ";
cout << "Contents of Element within Array is=" << alphabet[i] << endl;
}
}
return 0;
} //End of Int Main
1(第一部分输出字母表,2(接下来它提示输入一个数字并给出相关的字母,3(接下来程序应该输出,其他每个字母都是x,例如A x C x E x.......。4(最后,它应该提示用户选择O以从A或1开始字母表并启动B,然后它将每隔一个字母输出一次。最后一部分不起作用。它不提供选项,而是每次都做同样的事情。我已经以其他方式对其进行了调整,它两次都会做一个或另一个,而不是一个或另一个。我做错了什么?
对于第二部分更改
if (selection = 0){
自
if (selection == 0){
表达式selection = 0
是一个赋值,但您需要比较。
此外,为了检查均匀度,您可以使用%
运算符。 例如:
if (selection % 2 == 0)
{
// if even
}
else
{
// if odd
}
编辑:
为了避免将来出现此类错误类型,请训练自己在比较运算符的左侧写入常量值,例如
if (0 == selection)
如果你输入错误,编译器将显示有关L值的错误,该桅杆是可变的。
编辑 2(关于开关和用于(:
在下面的代码中
switch (selection)
{
case 0:
{
for (i = 0; i < size; i += 2)
{
cout << i << " ";
break;
}
}
case 1:
...
}
break
语句指的是for
,而不是switch
,因此:
1( for
将只执行一次;
2(虽然selection
0
case 1
无论如何都会在case 0
后工作。
正确的代码必须如下所示
switch (selection)
{
case 0:
{
for (i = 0; i < size; i += 2)
{
cout << i << " ";
}
break;
}
case 1:
...
}
请进行更改#
if (selection == 0)
我认为上述更改可以解决问题。在您的代码中,您不是比较而是在其中分配 0,因此永远不会希望 if 被执行并放入赋值如果不是标准。
希望这会有所帮助。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 当我的 if 条件计算结果为 false 时,我的 else 块将不会执行
- Fmod 函数清楚地输出一个预期的双精度值,但 if(fmod == 预期的双精度值)的计算结果不是 true
- 调用一个小函数两次(例如在if条件和主体中)比将结果存储在局部变量中更可取
- 如何让编译器忽略这个计算结果为 false 的 if-constexpr?
- 使用嵌套 if 语句的意外结果
- if, else if, else 函数未准确显示结果
- 循环中的IF-ELSE条件:未来的迭代覆盖以前的迭代结果
- 当我使用if语句时,为什么该程序会给我不同的结果
- "if"条件的计算结果始终为 "true"
- if(!结果)返回false;的C 糖语法;
- 为什么我的 if/else 语句没有产生预期的结果?
- if/else优化测试结果令人震惊
- 在语句中调用 std::set 函数时出现意外"if"评估结果
- 使用if循环排序时出现意外结果-糟糕的算法
- 对于循环数组迭代,if循环测试索引是否为真/假,计算结果不正确
- If语句和大括号.有/没有不同的结果
- 检查结果时减少if语句
- 使用'if'语句和"<<"时的结果错误,'>>'运算符比较 3 个整数