在 C++ 中将 Char 转换为 Int 的问题
Problems Converting Char to Int in C++
我正在努力让这款剪刀石头布游戏正常运行,但我无法通过“您已进入队列”,因为它会在那之后终止。
我的教授说我的问题是 userChoice 是 char 类型,而 randNumGenerated 是 int 类型。我尝试使用以下方法转换我的 r、p 和 s 值:
字符 r = 'a';
int a = r;
但编译器出现“重新定义:不同的基本类型”错误。我不确定该怎么做,因为将变量重新定义为 int 是我的意图。我认为这一切都错了吗?任何帮助将不胜感激!
int main()
{
char userChoice;
int computer;
int randNumGenerated = 0;
int r = 0;
int p = 0;
int s = 0;
unsigned seed;
cout << "Chose either rock, paper, or scissors" << endl;
cout << "Let r,p,and s represent rock,paper,and scissors respectively " << endl;
cin >> userChoice;
cout << "You entered: " << userChoice << endl;
cout << " " << endl;
seed = time(0);
srand(seed);
randNumGenerated = rand() % 3;
r == 0;
p == 1;
s == 2;
if ((userChoice == 0 && randNumGenerated == 2) || (userChoice == 1 && randNumGenerated == 0) || (userChoice == 2 && randNumGenerated == 1))
{
cout << "You win!";
}
if ((userChoice == 0 && randNumGenerated == 1) || (userChoice == 1 && randNumGenerated == 2) || (userChoice == 2 && randNumGenerated == 0))
{
cout << "You lose!";
}
if ((userChoice == 0 && randNumGenerated == 0) || (userChoice == 1 && randNumGenerated == 1) || (userChoice == 2 && randNumGenerated == 2))
{
cout << "It's a draw!";
}
return 0;
}
这三行没有效果(从字面上看,编译器应该警告你这一点):
r == 0;
p == 1;
s == 2;
您可能正在寻找类似的东西:
int userNum = -1;
switch (userChoice) {
case 'r':
userNum = 0;
break;
case 'p':
userNum = 1;
break;
case 's':
userNum = 2;
break;
}
但是,你为什么还要使用整数呢?如果您使用字符表示,您的 if 语句将更易于阅读:
char randomChosen = "rps"[randNumGenerated];
if ((userChoice == 'r' && randomChosen == 's') || ...) {
std::cout >> "You win!\n";
}
光是看这个就知道是剪刀石头布了。在您基于数字的代码中,我必须回顾一下转换 table.
我正在努力让这款剪刀石头布游戏正常运行,但我无法通过“您已进入队列”,因为它会在那之后终止。
我的教授说我的问题是 userChoice 是 char 类型,而 randNumGenerated 是 int 类型。我尝试使用以下方法转换我的 r、p 和 s 值: 字符 r = 'a'; int a = r;
但编译器出现“重新定义:不同的基本类型”错误。我不确定该怎么做,因为将变量重新定义为 int 是我的意图。我认为这一切都错了吗?任何帮助将不胜感激!
int main()
{
char userChoice;
int computer;
int randNumGenerated = 0;
int r = 0;
int p = 0;
int s = 0;
unsigned seed;
cout << "Chose either rock, paper, or scissors" << endl;
cout << "Let r,p,and s represent rock,paper,and scissors respectively " << endl;
cin >> userChoice;
cout << "You entered: " << userChoice << endl;
cout << " " << endl;
seed = time(0);
srand(seed);
randNumGenerated = rand() % 3;
r == 0;
p == 1;
s == 2;
if ((userChoice == 0 && randNumGenerated == 2) || (userChoice == 1 && randNumGenerated == 0) || (userChoice == 2 && randNumGenerated == 1))
{
cout << "You win!";
}
if ((userChoice == 0 && randNumGenerated == 1) || (userChoice == 1 && randNumGenerated == 2) || (userChoice == 2 && randNumGenerated == 0))
{
cout << "You lose!";
}
if ((userChoice == 0 && randNumGenerated == 0) || (userChoice == 1 && randNumGenerated == 1) || (userChoice == 2 && randNumGenerated == 2))
{
cout << "It's a draw!";
}
return 0;
}
这三行没有效果(从字面上看,编译器应该警告你这一点):
r == 0;
p == 1;
s == 2;
您可能正在寻找类似的东西:
int userNum = -1;
switch (userChoice) {
case 'r':
userNum = 0;
break;
case 'p':
userNum = 1;
break;
case 's':
userNum = 2;
break;
}
但是,你为什么还要使用整数呢?如果您使用字符表示,您的 if 语句将更易于阅读:
char randomChosen = "rps"[randNumGenerated];
if ((userChoice == 'r' && randomChosen == 's') || ...) {
std::cout >> "You win!\n";
}
光是看这个就知道是剪刀石头布了。在您基于数字的代码中,我必须回顾一下转换 table.