【发布时间】:2023-03-04 17:10:01
【问题描述】:
简单的石头、剪刀、纸程序。
我收到错误消息:[Error] lvalue required as left operand of assignment. 我不知道出了什么问题,因为我是 C++ 编程新手,为什么它不接受 if 语句?
// Globale variabler
int PC, User; // For å sammenligne input
int Uavgjort = 0;
int user_score = 0;
int pc_score = 0;
int RandRange( int low, int high );
void Compare_values();
int main()
{
srand( time( NULL ) );
cout << "Valg:" << '\n';
cout << "1. Stein" << '\n'
<< "2. Saks" << '\n'
<< "3. Papir" << '\n' << endl;
while ( cin >> User )
{
RandRange( 1, 3 );
Compare_values(); // sammenligner brukers verdi og random verdi
if ( user_score >= 10 || pc_score >= 10 )
break;
} // while slutter her
cout << "Game over!" << "\n\n";
cout << "Score: \n"
<< "PC Wins: " << pc_score << " times" << '\n'
<< "You won: " << user_score << " times" << '\n' << endl;
cin.get();
}
int RandRange( int low, int high )
{
PC = rand() % ( high - low + 1 ) + low;
return PC;
}
void Compare_values()
{
if ( User == 1 && PC == 1 )
{
cout << "Stein vs. Stein" << '\n';
cout << "Uavgjort!" << '\n';
++Uavgjort;
}
else if ( User == 1 && PC == 2 )
{
cout << "Stein vs. Saks" << '\n';
cout << "Du vant!" << '\n';
++user_score;
}
else if ( User == 1 && PC == 3 )
{
cout << "Stein vs. Papir" << '\n';
cout << "Dessverre, du tapte." << '\n';
++pc_score;
}
else if ( User == 2 && PC = 1 )
{
cout << "Saks vs. Stein" << '\n';
cout << "Dessverre, du tapte." << '\n';
++pc_score;
}
else if ( User == 2 && PC == 2 )
{
cout << "Saks vs. Saks" << '\n';
cout << "Uavgjort!" << '\n';
++Uavgjort;
}
else if ( User == 2 && PC == 3 )
{
cout << "Saks vs. Papir" << '\n';
cout << "Du vant!" << '\n';
++user_score;
}
else if ( User == 3 && PC == 1 )
{
cout << "Papir vs. Stein" << '\n';
cout << "Du vant!" << '\n';
++user_score;
}
else if ( User == 3 && PC == 2 )
{
cout << "Papir vs. Saks" << '\n';
cout << "Dessverre, du tapte." << '\n';
++pc_score;
}
else if ( User == 3 && PC == 3 )
{
cout << "Papir vs. Papir" << '\n';
cout << "Uavgjort!" << '\n';
++Uavgjort;
}
else
{
cout << "Jeg tror du har tastet feil verdi.." << '\n';
}
}
【问题讨论】:
-
首先,阅读完整的错误以找出问题的在哪里。您可以通过构建一个字符串然后打印它来减少行/else-if 语句的数量,并处理寻找获胜者的逻辑略有不同。这将帮助您避免诸如
=和==混淆的错误。 -
在哪一行?你有错误