一袭白衣梅子酒 2022-04-10 15:49 采纳率: 100%
浏览 22
已结题

Unity&&C#,简单选择问题,输出的Console调试日志和书上不一致

代码输出结果和《c#实践入门:快捷学习c#编程和unity游戏开发 第四版》书上的不匹配
public class LearningCurve : MonoBehaviour
{

    public bool pureOfHeart = true;
    public bool hasSecretIncantation = false;
    public string rareItem = "RelicStone";
    // Start is called before the first frame update
    void Start()
    {
        OpenTreasureChamber();
    }
    public void OpenTreasureChamber()
    {
        if (pureOfHeart && rareItem == "RelicStone")
        {
            if(!hasSecretIncantation)
            {
                Debug.Log("You have the spirit,but not the knowledge.");
            }
            else
            {
                Debug.Log("The treasure is yours,worthy hero");
            }
        }
        else
        {
            Debug.Log("Come back when you have what it takes");

        }
    }

}
运行结果

Come back when you have what it takes
书上是
You have the spirit,but not the knowledge.

经过测试,发现判断rareItem == "RelicStone"的值为假,为什么会是假?
  • 写回答

1条回答 默认 最新

  • Delevea 2022-04-10 23:40
    关注

    首先要理解C#中是怎么通过==来比较两个字符串是否相等
    ==操作是先判断两者内存地址是否相同,相同则返回true,不同,则继续判断值是否相同。
    所以你这里rareItem == "RelicStone"为什么是false,请仔细检查Inspector面板,是否在Editor中对变量进行了修改

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月19日
  • 已采纳回答 4月11日
  • 修改了问题 4月10日
  • 创建了问题 4月10日

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料