2011-10-02 2 views
0

이 프로그램은 첫 번째 "if"문에서 계속 분해되어 계속 caragory B에 연결됩니다. 어떤 코드를 추가하거나 제거해야합니까?프로그램이 "if"문에서 떨어져서 범주 "B"를 강제 실행합니다.

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 = 'A'; 
     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)"); 
        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)"); 
     } 
       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)"); 
        if (surcharge==Y) 
        { 
         cost = cost + 5.00; 

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


        } 
         if (surcharge==B) 


          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)"); 
        } 
       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)"); 
        if (surcharge==Y) 
        { 
         cost = cost + 8.00; 

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



       if (surcharge==B) 

        Console.Write("please enter the weight in pounds"); 
       LB = double.Parse(Console.ReadLine()); 
       cost = 3.00 * LB; 
       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() 
}//End class Program 
+1

어떻게 실패할까요? – Clive

+0

"A"를 입력하면 여전히 "B"가됩니다. – Jordan

+2

매우 긴 방법입니다. 간단한 방법으로 분해하고 단위 테스트를 사용하여 각각의 작은 메서드가 제대로 작동하는지 확인하는 것이 좋습니다. –

답변

0

A는 INT 변수 당신이 당신은 아마 할 의미 1. 값이 할당되는 : 경우 (catagory == 'A') 하지 경우 (catagory ==를 A)

+0

짐 당신은 문제 감사를 발견했을 것입니다! – Jordan

+0

대문자 또는 소문자를 테스트 할 수도 있습니다 (예 : if ((catagory == 'A') || (catagory == 'a')). 또한 사용자 입력을 요구하지 않습니다. if == Y) Y가 아닌 'Y'를 테스트해야합니다. –

관련 문제