-2

显示最终分数后,它不显示智商结果。请帮忙。这是完整的程序...我用箭头显示拒绝编译的开关块

class Class1
{
    public static int attempt, sum, AptScore, GenScore, MathScore, EngScore, bonus, TotalScore, FinalScore, choice = 0;
    public static string ans;
    static void Main(string[] args)
    {

           int FinalCount, AptCount = 0, EngCount = 0, MathCount = 0, GenCount = 0;

           Console.WriteLine("Welcome to Salisbury University IQ Test game \n=====================================");
           Console.WriteLine();
           Console.WriteLine("How many times have you attempted this test?");
           attempt = Convert.ToInt32(Console.ReadLine());


           while (true)
           {
               if (attempt > 1)
               {
                   Console.WriteLine("You cannot take this test");
                   break;
               }

               while (true)
               {

                   FinalCount = AptCount + EngCount + MathCount + GenCount;
                   if (FinalCount < 4)
                   {

                       Console.WriteLine("Salisbury University IQ Test game \n========================================");
                       Console.WriteLine("Press \n1. Aptitude \n2. English. \n3. Math \n4. Gk \n5. Exit");
                       choice = Convert.ToInt32(Console.ReadLine());

                       switch (choice)
                       {
                           case 1:
                               if (AptCount > 0)
                               {
                                   Console.WriteLine("THIS QUESTION HAS BEEN ATTEMPTED!!!");
                                   Console.WriteLine();
                                   continue;

                               }
                               Console.WriteLine(" What was the name of the lebanon tyrant who ruled for years unending before he was toppled due to civil war? \nA. Osama Bin laden \nB. Gaddafi \nC. Jonathan ");
                               ans = Console.ReadLine();
                               if (ans == "B" || ans == "b")
                               {
                                   AptScore += 10;
                               }
                               AptCount++;
                               continue;

                           case 2:
                               if (EngCount > 0)
                               {
                                   Console.WriteLine("THIS QUESTION HAS BEEN ATTEMPTED!!!");
                                   Console.WriteLine();
                                   continue;
                               }
                               Console.WriteLine(" What is the antonym of Pleasure? \nA. Pain \nB. Ecstacy \nC. Wonder");
                               ans = Console.ReadLine();
                               if (ans == "A" || ans == "a")
                               {
                                   EngScore += 10;
                               }
                               EngCount++;
                               continue;


                           case 3:
                               if (MathCount > 0)
                               {
                                   Console.WriteLine("THIS QUESTION HAS BEEN ATTEMPTED!!!");
                                   Console.WriteLine();
                                   continue;
                               }
                               Console.WriteLine(" What is the sum of 435 and 345? \nA. 799 \nB. 780 \nC. 600 ");
                               ans = Console.ReadLine();
                               if (ans == "B" || ans == "b")
                               {
                                   MathScore += 10;
                               }
                               MathCount++;
                               continue;

                           case 4:
                               if (GenCount > 0)
                               {
                                   Console.WriteLine("THIS QUESTION HAS BEEN ATTEMPTED!!!");
                                   Console.WriteLine();
                                   continue;
                               }
                               Console.WriteLine(" What year did Nigeria become a republic? \nA. 1960 \nB. 1963 \nC. 1990 ");
                               ans = Console.ReadLine();
                               if (ans == "B" || ans == "b")
                               {
                                   GenScore += 10;
                               }
                               GenCount++;
                               continue;

                           case 5:
                               Environment.Exit(0);
                               break;

                           default:
                               Console.WriteLine("You entered an invalid number");
                               continue;
                       } // end of switch

                   break;
               }  // end of inner while loop
                   break;
            } // end of else

              break;
           } // outer loop end

           if (attempt < 5 && attempt != 0)
           {
           TotalScore = MathScore + GenScore + EngScore + AptScore;
           Console.WriteLine("Your total score is : " + TotalScore);

           if (TotalScore == 10)
           {
               Console.WriteLine(" You have no Bonus point ");
           }
           else if (TotalScore == 20)
           {
               bonus += 2;
               Console.WriteLine("Your Bonus is {0}", bonus);
           }
           else if (TotalScore == 30)
           {
               bonus += 5;
               Console.WriteLine("Your Bonus is {0}", bonus);

           }
           else if (TotalScore == 40)
           {
               bonus += 10;
               Console.WriteLine("Your Bonus is {0}", bonus);

           }

            FinalScore = TotalScore + bonus;
               Console.WriteLine("Your finalscore is : " + FinalScore);

**it refuses to compile this switch block -->**  switch (FinalScore)
               {
                   case 10:
                       if (FinalScore >= 10)
                       {
                           Console.WriteLine("Your IQ level is below average");
                       }
                       break;

                   case 22:
                       if (FinalScore >= 22)
                       {
                           Console.WriteLine("Your IQ level is average");
                       }
                       break;
                   case 35:
                       if (FinalScore >= 35)
                       {
                           Console.WriteLine("You are intelligent");
                       }
                       break;
                   case 40:
                       if (FinalScore == 40)
                       {
                           Console.WriteLine("You are a genius");
                       }
                       break;

                   default:
                       break;

               } // end of last s1witch case

           }// end of if statement
       } // end of main method */


    } // end of class
4

3 回答 3

1

你应该使用 If else 语句来做你想做的事情,你可以通过使用像这样的三元语句来缩短你的代码,

 Console.WriteLine((FinalScore >= 40) ? "You are a genius" :
                 (FinalScore >= 35) ? "You are intelligent" :
                 (FinalScore >= 22) ? "Your IQ level is average" :
                 (FinalScore >= 10) ? "Your IQ level is below average" : "You are really below average");
//The really below average would be your something else referenced above.
于 2013-08-05T21:39:47.710 回答
1

对于您的特定情况,使用 if/else 总是更好,使用 switch 语句,您不能在情况下设置条件。看起来您正在检查范围,如果范围是恒定的,那么您可以尝试以下操作

                   if (FinalScore == 40)
                   {

                       Console.WriteLine("You are a genius");
                   }else  if (FinalScore >= 35)
                   {
                       Console.WriteLine("You are intelligent");

                   }else if (FinalScore >= 22)
                   {
                       Console.WriteLine("Your IQ level is average");

                   }else if(FinalScore >= 10)
                   {
                       Console.WriteLine("Your IQ level is below average");         
                   }else
                   {
                       // something else
                   }
于 2013-08-05T21:28:36.960 回答
0

您的代码编译得很好。

如果您记录了该 switch 块的默认情况,您会发现 40 实际上是一个无法访问的情况。您的公式保证永远不会生成 40 - 由于奖励积分,所有 4 个正确都是 50 分。所有不正确的也永远不会产生结果。

有效的情况是: 0, 10, 22, 35, 50。你只占10, 22, & 35

default:
    // this would've thrown an exception for FinalScore==50
    throw new Exception("unexpected score of " + FinalScore);
于 2013-08-05T21:54:14.620 回答