问题描述
当我试图运行这个游戏,想要结束它通过键入slut或slut(瑞典语为'end')它使一个新的播放器调用slut或荡妇 BUT 强>当我只有一个语句在while循环或If语句它都工作。
为了使它更清楚它工作时,我有(playerName!=Slut)但我想安全有(playerName! (playerName!=slut),然后它不工作。
when I try to run this game and wants to end it by typing "slut" or "Slut" (Swedish for 'end') it makes a new player call "slut" or "Slut" BUT when I only have ONE statement in the while loops or If statements it's all working. To make it more clear It works when I have (playerName != "Slut") but I want for safty have (playerName != "Slut") || (playerName != "slut") and then it wont work.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main(){
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;
}
推荐答案
您必须使用&&&
,而不是 ||
p>
You have to use &&
instead of ||
.Its supposed to be
if ((playerName != "Slut") && (playerName != "slut"))
和
while ((playerName != "Slut") && (playerName != "slut"))
如果
s和而
条件具有!=
( playerName!=Slut
和 playerName!=slut
)
in all the if
s and while
conditions having !=
(playerName != "Slut"
and playerName != "slut"
)
这篇关于如果语句在c ++,freaks out的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!