【问题标题】:What am I doing wrong with this "If" statement?我对这个“如果”语句做错了什么?
【发布时间】:2013-07-27 01:20:38
【问题描述】:

所以,我有两个 if 语句来检测您使用的是 HTC 8X 还是 8S,根据手机提供的 DeviceName。问题是它似乎忽略了“如果”并只运行所有代码......例如,我放置了一行代码(用于调试),一旦它运行每个部分就会显示一个消息框,但是两个消息框都显示了,而不仅仅是 8X,显示它正确检测到我正在 8X 上运行应用程序。这可能只是一个“愚蠢的错误”,但我无法弄清楚......

  if (Microsoft.Phone.Info.DeviceStatus.DeviceName == "Windows Phone 8X by HTC") ;
            {
                //Debugging MSG
                MessageBox.Show("8X Works")
                //Rating
                MainScore.Text = "6.1";
                //Subscores
                Processor.Text = "5.2";
                RAM.Text = "6.5";
                Graphics.Text = "8.0";
                HardDisk.Text = "5.1";
                //Issues
                Issues.Text = "0 ISSUES FOUND";
            }
            if (Microsoft.Phone.Info.DeviceStatus.DeviceName == "Windows Phone 8S by HTC");
            {
                //Debugging MSG
                MessageBox.Show("8S Works")
                //Rating
                MainScore.Text = "2.8";
                //Subscores
                Processor.Text = "3.2";
                RAM.Text = "2.4";
                Graphics.Text = "4.0";
                HardDisk.Text = "1.9";
                //Issues
                Issues.Text = "0 ISSUES FOUND";
            }

【问题讨论】:

    标签: c# windows windows-phone-8


    【解决方案1】:

    每次 IF 测试后立即删除分号。分号后面的代码块每次都无条件运行。

    【讨论】:

      【解决方案2】:

      因为有分号,所以编译器如何看待您的代码。
      (查找/* */ cmets)

      if (Microsoft.Phone.Info.DeviceStatus.DeviceName == "Windows Phone 8X by HTC")
          /* DO NOTHING */;
      
      /* Regular, Unconditional Code */
      {
          //Debugging MSG
          MessageBox.Show("8X Works")
          //Rating
          MainScore.Text = "6.1";
          //Subscores
          Processor.Text = "5.2";
          RAM.Text = "6.5";
          Graphics.Text = "8.0";
          HardDisk.Text = "5.1";
          //Issues
          Issues.Text = "0 ISSUES FOUND";
      }
      
      if (Microsoft.Phone.Info.DeviceStatus.DeviceName == "Windows Phone 8S by HTC")
          /* DO NOTHING */;
      
      /* Regular Unconditional code */
      {
         //Debugging MSG
         MessageBox.Show("8S Works")
         //Rating
         MainScore.Text = "2.8";
         //Subscores
         Processor.Text = "3.2";
         RAM.Text = "2.4";
         Graphics.Text = "4.0";
         HardDisk.Text = "1.9";
         //Issues
         Issues.Text = "0 ISSUES FOUND";
       }
      

      【讨论】:

        猜你喜欢
        • 2021-09-17
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        • 1970-01-01
        相关资源
        最近更新 更多