下面是我的C++程序,可是为什么当输入Q和Z的时候执行不正确呢?
来源:百度知道 编辑:UC知道 时间:2024/07/16 03:53:22
这个程序是当输入一个电话键盘上的一个字母时,提示该键盘所对应的数字。源程序如下:
# include <iostream>
using namespace std;
int main()
{
char a_char;
cout <<"Enter a single letter, and I will tell you what the corresponding digit is on the telephone."<< endl;
cin >> a_char;
if ( (a_char>='A' && a_char<='Z') && (a_char !='Q' || a_char!='Z') )
{ if (a_char=='A' || a_char=='B'|| a_char=='C')
cout << a_char << endl << "The digit 2 corresponds to the letter " << a_char <<" on the telephone."<< endl;
else if (a_char== 'D' || a_char== 'E' || a_char== 'F' )
cout << a_char << endl << "The digit 3 corresponds to the letter " << a_char <<" on the telephone."&l
# include <iostream>
using namespace std;
int main()
{
char a_char;
cout <<"Enter a single letter, and I will tell you what the corresponding digit is on the telephone."<< endl;
cin >> a_char;
if ( (a_char>='A' && a_char<='Z') && (a_char !='Q' || a_char!='Z') )
{ if (a_char=='A' || a_char=='B'|| a_char=='C')
cout << a_char << endl << "The digit 2 corresponds to the letter " << a_char <<" on the telephone."<< endl;
else if (a_char== 'D' || a_char== 'E' || a_char== 'F' )
cout << a_char << endl << "The digit 3 corresponds to the letter " << a_char <<" on the telephone."&l
if ( (a_char>=\'A\' && a_char<=\'Z\') && (a_char !=\'Q\' || a_char!=\'Z\') ) 这句改为
if ( (a_char>=\'A\' && a_char<=\'Z\') && (a_char !=\'Q\' && a_char!=\'Z\') ) ,
你想输入Q或Z时推出程序,但是你想想,当输入Q时,a_char !=\'Q\' 是false,但a_char!=\'Z\'是true,两个相 || 后的结果是 true,所以得不到正确的结果
if ( (a_char>='A' && a_char<='Z') && (a_char !='Q' || a_char!='Z') )
应改为
if ( (a_char>='A' && a_char<='Z') && (a_char !='Q' && a_char!='Z') )
有必要这么麻烦吗?
if ( (a_char>=\'A\' && a_char<=\'Z\') && (a_char !=\'Q\' || a_char!=\'Z\') )
应改为
if ( (a_char>=\'A\' && a_char<=\'Z\') && (a_char !=\'Q\' && a_char!=\