我已经尝试过所有可以让它发挥作用的东西。我已经研究了这么多,并尝试了我能找到的所有可能的解决方案。我在Q ++中使用QtCreator和编码。
int main() {
int Die1, Die2;
cout << "Die 1: ";
cin >> Die1;
cout << "Die 2: ";
cin >> Die2;
while ( (Die1+Die2 != '2')||(Die1+Die2 != '3')||(Die1+Die2 != '5')||(Die1+Die2 != '7')||(Die1+Die2 != '9')||(Die1+Die2 != '11')) {
cout << "You Rolled a " <<Die1<< " and " <<Die2<< " for a Total of " <<Die1 + Die2<<". Please Roll Again\n";
cout << "Die 1: ";
cin >> Die1;
cout << "Die 2: ";
cin >> Die2;
}
答案 0 :(得分:3)
您的代码:
(Die1+Die2 != '2') ||
(Die1+Die2 != '3') ||
(Die1+Die2 != '5') ||
(Die1+Die2 != '7') ||
(Die1+Die2 != '9') ||
(Die1+Die2 != '11')
将评估为:
A.
( 1+1 != 2 ) = false
( 1+1 != 3 ) = true
break;
B.
( 1+2 != 2 ) = true
break;
C.
( 1+3 != 2 ) = true
break;
...
( 1 + ( x>2 ) != 2 ) = true
这里的逻辑本质上存在缺陷,因为它总是评估为 true 。
修改的
正如其他人所指出的那样,您也在尝试将这些整数与其char
值进行比较,这些值与您期望的值不符。
'0' = (hex)0x30 = (int)48
'1' = (hex)0x31 = (int)49
'2' = (hex)0x32 = (int)50
...
要解决此问题,您应该与实际数字进行比较:
( Die1 + Die2 != 2 )
答案 1 :(得分:1)
你可能想检查一下你的状况!
由于条件是否定的,你可以使用AND(&amp;&amp;)而不是OR(||)来组合它们。
修改强> 正如所指出的,你应该将Die1和Die2与数字进行比较。如果您想保留字符,可以添加字符&#39; 0&#39; 0到Die1和Die2的总和。
这似乎工作正常:
#include <bits/stdc++.h>
using namespace std;
int main() {
int Die1, Die2;
cout << "Die 1: ";
cin >> Die1;
cout << "Die 2: ";
cin >> Die2;
while ( (Die1+Die2 != 2)&&(Die1+Die2 != 3)&&(Die1+Die2 != 5)&&(Die1+Die2 != 7)&&(Die1+Die2 != 9)&&(Die1+Die2 != 11)) {
cout << "You Rolled a " <<Die1<< " and " <<Die2<< " for a Total of " <<Die1 + Die2<<". Please Roll Again\n";
cout << "Die 1: ";
cin >> Die1;
cout << "Die 2: ";
cin >> Die2;
}
}