【问题标题】:Converting String to Int in C# in Visual Studio [closed]在 Visual Studio 中的 C# 中将字符串转换为 Int [关闭]
【发布时间】:2018-02-21 00:08:23
【问题描述】:

我正在尝试制作一个石头、剪纸、剪刀游戏,我想我差不多完成了,但我不断收到“无法将类型“字符串”隐式转换为“整数”。我已经浏览了这个网站,但我不知道如何修复它。这是我的代码

    int userChoice = int.Parse(RadioButtonList1.SelectedValue);


    Random r = new Random();
    int computerChoice = r.Next(3);

    if (computerChoice == 1)
    {
        if  (userChoice = "rock") {

            resultLabel.Text = Convert.ToString("Computer chose Rock too. Tie!");
        }
        if (userChoice = "paper")
        {
            resultLabel.Text = "Computer chose Rock. You win!";
        }
        if (userChoice = "scissors")
        {
            resultLabel.Text = "Computer chose Rock. You Lose!";
        }
    }

    else if (computerChoice == 2)
    {
        if(userChoice = "rock") {
            resultLabel.Text = "Computer chose Rock. You win!";
        }
        if (userChoice = "paper")
        {
            resultLabel.Text = "Computer chose Paper too. Tie!";
        }
        if (userChoice = "scissors")
        {
            resultLabel.Text = "Computer chose Scissors. You lose!";
        }
    }

    else if (computerChoice == 3)
    {
        if (userChoice = "rock")
        {
            resultLabel.Text = "Computer chose Rock. You Lose!";
        }
        if (userChoice = "paper")
        {
            resultLabel.Text = "Computer chose Paper. You win!";
        }
        if (userChoice = "scissors")
        {
            resultLabel.Text = "Computer chose Scissors too. Tie!";
        }
    }


}
}

【问题讨论】:

  • 你为什么要在string上打电话给Convert.ToString()?
  • 这条线userChoice = "rock" 有几点是错误的。首先,userChoice 是一个 int,第二个 '=' 是一个赋值运算符(你想要相等运算符 ==)。您收到错误消息的原因是因为您基本上试图将字符串分配给 int。
  • 我可以看到您的代码存在一些问题。第一个问题是 userChoice 被定义为 int 并且您尝试与多个 if 语句中的字符串进行比较。第二个问题是(我希望这只是一个错字)是几个if 语句正在执行赋值操作而不是比较操作。您可能想使用enum 来清理您的逻辑。
  • 我们不知道如何帮助您,因为我们不知道 RadioButtonList1.SelectedValue 包含什么。它包含什么?
  • 你应该指出你得到异常的地方

标签: c# string visual-studio int


【解决方案1】:

首先
您需要将所有相等检查更改为 == 而不是 =。
= 分配一个值
== 是一个相等运算符

if(x == y)

其次
您正在尝试将 int 与 "Scissor/Rock/Paper" 进行比较,这是不可能的。 我建议您创建与您的价值观相匹配的枚举

public enum Choice
{
    Rock = 1,
    ...
}
if ((Choice)userChoice == Choice.Scissor) //Or cast anywhere else where you "input" is an int - you can always cast to an enum from an int

并与之比较而不是字符串

【讨论】:

  • 虽然这并没有错,但这只是 OP 代码存在的众多问题之一。如果您决定回答这个边缘题外的问题,那么至少要尝试涵盖所有这些问题。
  • 我实际上是在回答他的确切问题“无法将类型'string'隐式转换为'int'”
  • 这只是更改为不同的错误(无法将字符串与 int 进行比较)
  • 或者更好,Choice userChoice = (Choice) int.Parse(RadioButtonList1.SelectedValue);
【解决方案2】:

你可以试试这个代码

    string userChoice = RadioButtonList1.SelectedValue.ToString();
    Random r = new Random();
    int computerChoice = r.Next(3);

    if (computerChoice == 1)
    {
        if  (userChoice == "rock") {

            resultLabel.Text = "Computer chose Rock too. Tie!";
        }
        if (userChoice == "paper")
        {
            resultLabel.Text = "Computer chose Rock. You win!";
        }
        if (userChoice == "scissors")
        {
            resultLabel.Text = "Computer chose Rock. You Lose!";
        }
    }

    else if (computerChoice == 2)
    {
        if(userChoice == "rock") {
            resultLabel.Text = "Computer chose Rock. You win!";
        }
        if (userChoice == "paper")
        {
            resultLabel.Text = "Computer chose Paper too. Tie!";
        }
        if (userChoice == "scissors")
        {
            resultLabel.Text = "Computer chose Scissors. You lose!";
        }
    }

    else if (computerChoice == 3)
    {
        if (userChoice == "rock")
        {
            resultLabel.Text = "Computer chose Rock. You Lose!";
        }
        if (userChoice == "paper")
        {
            resultLabel.Text = "Computer chose Paper. You win!";
        }
        if (userChoice == "scissors")
        {
            resultLabel.Text = "Computer chose Scissors too. Tie!";
        }
    }

【讨论】:

  • 希望值实际上是字符串:)
  • 是的,我想是的,:) 我会喜欢的,呵呵
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 2015-08-02
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多