2013-11-10 80 views
-3

所以,我對整個C#語言有些新鮮,有人要求我們將其簡單的if else轉換爲開關。這就是我所知道的。這是對的嗎?如果不是,你能告訴我問題在哪裏嗎?還是有什麼缺失?這裏唯一的問題是開關盒的輸出沒有出現。C#開關盒不出現

static void Main(string[] args) 
    { 

     Console.Write("Enter Name: "); 
     string studname = Console.ReadLine(); 
     Console.Write("Enter Age: "); 
     string age = Console.ReadLine(); 
     Console.Write("Enter Year Level: "); 
     int level = int.Parse(Console.ReadLine()); 
     Console.Write("Enter Course: "); 
     string course = Console.ReadLine(); 
     Console.Write("Enter Score 1: "); 
     int num1 = int.Parse(Console.ReadLine()); 
     Console.Write("Enter Score 2: "); 
     int num2 = int.Parse(Console.ReadLine()); 
     Console.Write("Enter Score 3: "); 
     int num3 = int.Parse(Console.ReadLine()); 
     int total = num1 + num2 + num3; 
     int ave = (num1 + num2 + num3)/3; 
     int rate = ave; 




     Console.WriteLine(""); 
     Console.WriteLine(""); 
     Console.WriteLine(""); 
     Console.Write("Student Name: " + studname); 
     Console.WriteLine(""); 
     Console.Write("Student Age: " + age); 
     Console.WriteLine(""); 
     Console.Write("Student Year Level: " + level); 
     Console.WriteLine(""); 
     Console.Write("Student Course: " + course); 
     Console.WriteLine(""); 
     Console.Write("Score 1: " + num1); 
     Console.WriteLine(""); 
     Console.Write("Score 2: " + num2); 
     Console.WriteLine(""); 
     Console.Write("Score 3: " + num3); 
     Console.WriteLine(""); 
     Console.Write("Total_Score= " + total); 
     Console.WriteLine(""); 
     Console.Write("Total_Average= " + ave); 


     switch (rate) 
     { 
      case 1: 
       if (rate < 100 && rate > 90) 

       Console.Write("Rate = 1.00"); 
       Console.WriteLine(""); 
       Console.Write("Hi! " + studname + "you Passed"); 
       Console.WriteLine(""); 
       break; 


      case 2: 
       if (rate < 89 && rate > 85) 

        Console.Write("Rate = 1.00"); 
       Console.WriteLine(""); 
       Console.Write("Hi! " + studname + "you Passed"); 
       Console.WriteLine(""); 
       break; 

      case 3: 
       if (rate < 84 && rate > 80) 

        Console.Write("Rate = 1.00"); 
       Console.WriteLine(""); 
       Console.Write("Hi! " + studname + "you Passed"); 
       Console.WriteLine(""); 
       break; 

      case 4: 
       if (rate < 79 && rate > 75) 

        Console.Write("Rate = 1.00"); 
       Console.WriteLine(""); 
       Console.Write("Hi! " + studname + "you Passed"); 
       Console.WriteLine(""); 
       break; 

      case 5: 
       if (rate < 75) 

        Console.Write("Rate = 5.00"); 
       Console.WriteLine(""); 
       Console.Write("Hi! " + studname + "you Failed"); 
       Console.WriteLine(""); 
       break; 


     } 





     Console.ReadLine(); 
    } 
+0

單詞'重構'和DRY春天想起來...... –

回答

1

您誤解了switch -block的作用。如果您正在測試的值僅使用幾個設置值中的一個,則會使用它。

試着擺脫整個switch版塊,然後重新編寫此代碼塊,以便使用一系列if/else代替。另請注意,您需要處理邊緣案例,如rate = 90;您需要檢查rate >= 90捕捉這種情況下:

if (rate < 100 && rate >= 90) 
{ 
    Console.Write("Rate = 1.00"); 
    Console.WriteLine(""); 
    Console.Write("Hi! " + studname + "you Passed"); 
    Console.WriteLine(""); 
} 
else if (rate < 89 && rate >= 85) 
{ 
    Console.Write("Rate = 1.00"); 
    Console.WriteLine(""); 
    Console.Write("Hi! " + studname + "you Passed"); 
    Console.WriteLine(""); 
} 
else ... 

或者,如果你真的想這樣做同樣的事情在每一種情況下,除非rate < 75,你可以簡化這個到:

if (rate >= 75) 
{ 
    Console.Write("Rate = 1.00"); 
    Console.WriteLine(""); 
    Console.Write("Hi! " + studname + "you Passed"); 
    Console.WriteLine(""); 
} 
else 
{ 
    Console.Write("Rate = 5.00"); 
    Console.WriteLine(""); 
    Console.Write("Hi! " + studname + "you Failed"); 
    Console.WriteLine(""); 
} 

甚至僅僅是這樣的:

Console.Write("Rate = {0}", (rate >= 75) ? "1.00" : "5.00"); 
Console.WriteLine(""); 
Console.Write("Hi! {0} you {1}", studname, (rate >= 75) ? "Passed" : "Failed"); 
Console.WriteLine(""); 
+0

它聽起來像OP開始與if語句,並希望把它寫成一個開關的情況下......這不是在這種情況下是有道理的。 – Harrison

0

你的情況下,值不正確,switch語句在這種情況下接通的情況下的值,所以,案例1只會在速率= 1時被調用,等等,所以你需要修改這些值才能輸出。