2011-10-03 55 views
-4

我找不到原因的情況下2,3不會讓我用其他的類別選擇,但它讓我在案例1中使用它。它在哪裏分崩離析?哪些代碼需要編輯?程序compiller不會讓我在過去的2例使用「其他」,我不明白爲什麼

我固定在別人的我的看法,但我得到了前控制不能從一個case標籤(「情形3:」)通過跌倒了,我從來沒有見過一個新的錯誤到另一個。這是什麼意思?

using System; 

class Program 
{ 
    enum Numbers { standard = 1, express = 2, same = 3 }; 

    const int A = 1, B = 2; 
    const int Y = 3, N = 4; 
    static void Main() 
    { 

     double cost, LB; 
     int Number_of_items; 
     int myNumbers; 
     char catagory; 
     char surcharge = 'Y'; 

     Console.WriteLine("please enter the type of shiping you want"); 
     Console.WriteLine("Enter 1:standard shipping."); 
     Console.WriteLine("Enter 2:express shipping."); 
     Console.WriteLine("Enter 3:same day shipping."); 

     myNumbers = int.Parse(Console.ReadLine()); 
     switch ((Numbers)myNumbers) 
     { 
      case Numbers.standard: 
       Console.WriteLine("thankyou for chooseing standerd shipping"); 
       Console.WriteLine("please choose a catagory"); 
       Console.Write("Type A or B to make your selection"); 
       catagory = char.Parse(Console.ReadLine()); 
       { 
        if (catagory == 'A') 
        { 
         Console.Write("please enter the number of items"); 
         Number_of_items = int.Parse(Console.ReadLine()); 
         cost = 3 * Number_of_items; 

         Console.Write("is this shipment going to alaska or Hawaii? (Y or N)"); 
         surcharge = char.Parse(Console.ReadLine()); 

         if (surcharge == 'Y') 
         { 
          cost = cost + 2.50; 

          Console.WriteLine("Total cost is {0}.", cost); 
         } 
         else 
          Console.WriteLine("total cost is {0}.", cost); 


        } 
        else 
         Console.Write("please enter the weight in pounds"); 
        LB = double.Parse(Console.ReadLine()); 
        cost = 1.45 * LB; 
        Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)"); 
       } surcharge = char.Parse(Console.ReadLine()); 
       if (surcharge == 'Y') 
       { 
        cost = cost + 2.50; 

        Console.WriteLine("Total cost is {0}.", cost); 
       } 
       else 
        Console.WriteLine("total cost is {0}.", cost); 

       break; 


      case Numbers.express: 
       Console.WriteLine("thankyou for chooseing Express Shipping"); 
       Console.WriteLine("please choose a catagory"); 
       Console.Write("Type A or B to make your selection"); 
       catagory = char.Parse(Console.ReadLine()); 

       if (catagory == 'A') 
       { 
        Console.Write("please enter the number of items"); 
        Number_of_items = int.Parse(Console.ReadLine()); 
        cost = 4 * Number_of_items; 
        { 
         Console.Write("is this shipment going to alaska or Hawaii? (Y or N)"); 
         surcharge = char.Parse(Console.ReadLine()); 
         if (surcharge == 'Y') 
         { 
          cost = cost + 5.00; 

          Console.WriteLine("Total cost is {0}.", cost); 
         } 
         else 
          Console.WriteLine("total cost is {0}.", cost); 


        } 
       } 
       else 
       { 
        { 

         Console.Write("please enter the weight in pounds"); 
         LB = double.Parse(Console.ReadLine()); 
         cost = 2.50 * LB; 
         Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)"); 
        } 
       } 

       surcharge = char.Parse(Console.ReadLine()); 
       if (surcharge == 'Y') 
       { 
        cost = cost + 5.00; 

        Console.WriteLine("Total cost is {0}.", cost); 
       } 
       else 
        Console.WriteLine("total cost is {0}.", cost); 
       break; 





      case Numbers.same: 
       Console.WriteLine("thankyou for chooseing Same Day Shipping"); 
       Console.WriteLine("please choose a catagory"); 
       Console.Write("Type A or B to make your selection"); 
       catagory = char.Parse(Console.ReadLine()); 
       if (catagory == 'A') 
       { 
        Console.Write("please enter the number of items"); 
        Number_of_items = int.Parse(Console.ReadLine()); 
        cost = 5.50 * Number_of_items; 

        Console.Write("is this shipment going to alaska or Hawaii? (Y or N)"); 
        surcharge = char.Parse(Console.ReadLine()); 
        if (surcharge == 'Y') 
        { 
         { 
          cost = cost + 8.00; 

          Console.WriteLine("Total cost is {0}.", cost); 
         } 
        } 
        else 
        { 
         Console.WriteLine("total cost is {0}.", cost); 
        } 


       } 
       else 
       { 

        { 
         Console.Write("please enter the weight in pounds"); 
         LB = double.Parse(Console.ReadLine()); 
         cost = 3.00 * LB; 
         surcharge = char.Parse(Console.ReadLine()); 
         Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)"); 
        } 
        if (surcharge == 'Y') 
        { 
         cost = cost + 8.00; 

         Console.WriteLine("Total cost is {0}.", cost); 
        } 
        else 
         Console.WriteLine("total cost is {0}.", cost); 

        break; 
       } 

       Console.ReadLine(); 

     }//End Main() 
    } 
} 
+1

你會得到什麼錯誤? – SLaks

+0

那裏有一些額外的{'從那裏我可以看到....不知道你爲什麼要嘗試範圍這些地區,這是很難看,因爲你的代碼格式不正確。 –

+1

你應該正確地縮進代碼(例如,使用Visual Studio的格式選項) – SLaks

回答

2

我試圖通過Lindent運行你的代碼來解決可怕的格式化並報告unmatched else。你在你的代碼:

indent: foo.c:93: Error:Unmatched 'else' 
indent: foo.c:139: Error:Unmatched 'else' 
indent: foo.c:164: Error:Unexpected end of file 

(是的,indent(1)不能用於C#太多else語句,但它的工作原理出奇地好於大多數類似C語言,在這種情況下所指出的至少兩個else聲明沒有正確匹配,不用擔心那些特定的行號 - 這個程序需要被正確格式化和打散成小塊 - 嘗試僅僅與幾個正確放置{} ISN bodge了一起「T正確答案。)

掰開你的巨大main()例行成更小的功能。 (您的代碼複製阿拉斯加/夏威夷問題倍 - 這應該是一個獨立的例行右邊有可能全部是/否的問題,應通過一個常規的處理:傳遞的問題,它會做提示並返回truefalse你)

我強烈建議您使用您else條款括號,如果你使用他們對你if條款:

這是罰款:

if (foo) 
    /* statement */ 
else 
    /* single statement */ 

但是,如果你曾經需要對第一塊使用大括號,然後用它們的else塊太:

if (foo) { 
    /* stuff */ 
    /* more stuff */ 
} else { 
    /* use those braces! */ 
    /* you will find reading your code far easier if you do */ 
} 

好運。

1

這裏有兩個問題。首先是一對不應該在那裏的大括號。即這些:

 catagory = char.Parse(Console.ReadLine()); 
     { // <------- THIS ONE 
      if (catagory == 'A') 
     // 
     // skipped code 
     // 

      Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)"); 
     } /* <------- THIS ONE */ surcharge = char.Parse(Console.ReadLine()); 
     if (surcharge == 'Y') 

第二個問題(也就是造成你控制無法通過錯誤屬於一個)是事實休息聲明的Numbers.same情況是語句的條件塊內(在你的if (catagory == 'A')的其他部分)。這意味着只有當類別與'A'不同時,它纔會被執行,而不是你變量的每個值。你只需要在大括號之後移動中斷。

相關問題