如何解决 "if" 语句中的 "unreachable code detected" 错误?

How do I solve the "unreachable code detected" error in "if" statement?


我是 C# 和编程的新手,目前正在从事名为 Marshals Revenue 的项目。我在 运行 程序时出错,如果有人可以帮助我理解问题。

它告诉我检测到关于“if”语句(错误 CS0162)的无法访问的代码,它不会让我 运行 代码的处理部分。我不确定为什么会收到错误消息,因为它看起来像是正确的语法。

我还被告知他们想要包括“CultureInfo.GetCultureInfo”方法。正确的格式是“WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));"。我不确定这是否与它不会 运行 我的“if”语句的原因有关,也不确定将 get culture 方法语句放在哪里。

下面是我正在使用的代码。

提前致谢。

using System;
using System.Globalization;
class MarshallsRevenue
{
   static void Main()
   {
     const int INTERIORPRICE= 500;
     const int EXTERIORPRICE=750; 
     string entryString;
     int numberInterior;
     int numberExterior;
     int revenueInterior;
     int revenueExterior;
     int total;
      bool isInteriorGreater;

     // declare the required variables
     bool valid;
     valid=true;
     int Month;
     int monthInterPrice=INTERIORPRICE;
     int monthExterPrice=EXTERIORPRICE;

     // Prompt the user to Enter the month 
     Console.WriteLine("Enter the number of month being scheduled >>");

     // Read the input
     entryString = Console.ReadLine();

     // convert the input to an integer
     Month = Convert.ToInt32(entryString);

     Console.WriteLine("Enter number of interior murals being scheduled >>");
     entryString = Console.ReadLine();
     numberInterior = Convert.ToInt32(entryString);
     Console.WriteLine("Enter number of exterior murals scheduled >>");
     entryString = Console.ReadLine();
     numberExterior = Convert.ToInt32(entryString);

     //use a switch case to perform the aciton
     //as per the entered month 
     switch(Month) {
      //set the exterior murals
      //to zero for the month
      //December through February 
      case 1: 
      case 2:
      case 12:
      numberExterior=0;
      break; 

      //if the month is either 
      //one of April, May, September
      //or October, reduce the price 
      //of exterior murals.

      case 4:
      case 5:
      case 9:
      case 10:
      monthExterPrice = 699;
      break;
      //if the month is either 
      //July or August
      //or October, reduce the price 
      //of interior murals.

      case 7:
      case 8:
      monthInterPrice = 450;
      break;

      //Do nothing for the months 
      //of March June and November.

      case 3:
      case 6:
      case 11:
      break;

      //if the entered month is invalid, 
      //display an error message and 
      //set the is valid month to false. 

      default: 
      Console.WriteLine("The entered month is invalid.");
      valid=false;
      break; 

      //if the entered month is valid 
      //perform the calculations and display
      //the results. 

      if(valid) 
      {
        revenueInterior = numberInterior * monthInterPrice;
        revenueExterior = numberExterior * monthExterPrice;
        total = revenueExterior + revenueInterior;
        isInteriorGreater = numberInterior > numberExterior;
        Console.WriteLine("{0} interior murals are scheduled at {1} each for a total of {2}", numberInterior, monthInterPrice.ToString("C"), revenueInterior.ToString("C"));
        Console.WriteLine("{0} exterior murals are scheduled at {1} each for a total of {2}", numberExterior, monthExterPrice.ToString("C"), revenueExterior.ToString("C"));
        Console.WriteLine("Total revenue expected is {0}", total.ToString("C"));
        Console.WriteLine("It is {0} that there are more interior murals sceduled than exterior ones.", isInteriorGreater);

        



      }




     }




   }
}

将 if 语句移到 default 或我认为是你的意图的 switch 语句中,因为忽略 break 关键字后的任何代码

您正在开关中使用 if 语句。该开关适用于案件。它检查 case 值,每个 case 后都有 break。所以你的代码不会执行 if 语句。

因此,如果您想始终执行 if 语句,则将其移出 switch.

无法访问 if 语句,因为它包含在 switch 语句的大括号中,但它不是任何 case 的一部分。

我相信你需要:

switch(Month) {
 //cases go here
}

if(valid) 
{
  //if stuff goes here
}

您可能需要考虑的 2 件事。

  1. 你可以写bool valid = true;
  2. 对于你什么都不做的月份,你可以简单地省略案例。