题目描述
输入一串符号(长度不大于50),依次输出每个符号值为偶数的ASCII值,值之间用逗号间隔。如果没有满足条件的,则输出 NO
例如,输入为:is1
ASCII 分别为 105,115,49
没有一个是偶数,则输出NO
例子输入
A1b+#
例子输出
98
我的输出有很多负52,而且一直输出NO
#include
using namespace std;
int main()
{
char arr[50];
cin >> arr;
int end;
end = sizeof(arr) / sizeof(arr[0]); //计算符号个数
bool none = true;
for (int i = 0; i <= end; i++) //i代表下标
{
if ((int)arr[i] % 2 == 0) //判断ASCII值为偶数的符号值
{
cout << (int)arr[i] << ",";
none = false;
}
}
cout << endl;
if (none = true)cout << "NO" << endl;
return 0;
}
if (none == true)
少写了个等号,就变成赋值语句啦
1.sizeof函数得出的是整个数组的大小,你在第6行开了一个长为50的数组,最终得到的end永远都等于50
2.第20行if后的括号里应该是用“==”而不是“=”
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char arr[50];
cin >> arr;
int len = strlen(arr);
bool none = true;
for (int i = 0; i < len; i++) //i代表下标
{
if (((int)arr[i]) % 2 == 0) //判断ASCII值为偶数的符号值
{
cout << (int)arr[i] << ",";
none = false;
}
}
cout << endl;
if (none)
cout << "NO" << endl;
return 0;
}