如何解决“ if”语句中的“检测到无法到达的代码”错误?

塞思·马特斯(Seth Mathes)

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

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

我还被告知他们想包含“ CultureInfo.GetCultureInfo”方法。正确的格式是“ WriteLine(“这是一个示例:{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语句不可访问,因为它包含在switch语句的花括号中,但不属于任何情况。

我相信您需要:

switch(Month) {
 //cases go here
}

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

您可能要考虑的2件事。

  1. 你可以写 bool valid = true;
  2. 在您什么都不做的几个月中,您可以简单地省略案例。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章