当我尝试运行此游戏并希望通过键入" slut"来结束它时或者" Slut" (瑞典人为'结束')它打了一个新的玩家电话" slut"或者" Slut" 但是当我在while循环或If语句中只有一个语句时,它都可以正常工作。 为了使它更清晰当我有(playerName!=" Slut")但我想要安全时有(playerName!=" Slut&#34) ;)|| (playerName!=" slut")然后它不会工作。
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main(){
//Declaration
setlocale(LC_ALL, "Swedish"); //Swedish characters
int dices;
int diceSum = 0;
int absoluteLowestSum = 999999999; //To get out the lowest sum I'm comparing to a big value
int diceRoll1 = 0;
int diceRoll2 = 0;
string playerName;
string playerLost;
//Plants a seed
srand((int)time(0));
//Instructions
cout << "Nu spelar vi ”Otur i tärning”: " << endl;
cout << "Hur många tärningskast ska göras per spelare?" << endl;
cin >> dices;
//If "slut"/""Slut" is written, while stops
while ((playerName != "Slut") || (playerName != "slut")){
cout << "Namnet på spelaren?";
cin >> playerName;
for (int i = 0; i < dices; i++){
if ((playerName != "Slut") || (playerName != "slut")){
diceRoll1 = rand() % 6 + 1;
diceRoll2 = rand() % 6 + 1;
cout << diceRoll1 << " " << diceRoll2 << " = " << diceRoll1 + diceRoll2 << endl;
diceSum = diceSum + diceRoll1 + diceRoll2;
}
}
if ((playerName != "Slut") || (playerName != "slut")){
while (diceSum < absoluteLowestSum){ //Give us the lowest sum
absoluteLowestSum = diceSum;
playerLost = playerName; //Give us the name who lost
}
cout << "Summan: " << diceSum << endl;
diceSum = 0; //Puts the sum to 0 when a new player enters his/her name
}
if ((playerName == "Slut") || (playerName == "slut")){
cout << playerLost << " hade mest otur och fick tärningssumman " << absoluteLowestSum << endl;
}
}
return 0;
}
答案 0 :(得分:0)
您必须使用&&
而不是||
。它应该是
if ((playerName != "Slut") && (playerName != "slut"))
和
while ((playerName != "Slut") && (playerName != "slut"))
在所有if
和while
条件下!=
(playerName != "Slut"
和playerName != "slut"
)