在我为学校学习而制作的这个简单问答问答游戏中,在实现 switch 和 case 时遇到了麻烦

问题描述

大家好,我是一名高中生,在过去的几个月里,他第一次学习/玩弄 C#。有一天,我想通过编写一个脚本来练习我在 c# 和学校中的技能,该脚本会打印出一个需要答案的单词(单词的翻译)。

这是当前状态的脚本:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string input = Console.ReadLine();

            int score = 0;
            bool accessKey = true;

            List<string> words = new List<string>();

            words.Insert(0,"word0");
            words.Insert(1,"word1");
            words.Insert(2,"word2");
            words.Insert(3,"word3");

            if (input == "start")
            {
                                                
                do
                {
                    Random rnd = new Random();
                    int whatWordisThis = rnd.Next(words.Count);
                    string printedWord = words[whatWordisThis];
                    Console.WriteLine(printedWord);
                    string input1 = Console.ReadLine();
                    
                    switch (printedWord)
                    {


                        case "word0":
                            
                            if (input1 == "translation0")
                            {
                                score++;
                                Console.WriteLine("Good job! \n \nYour current score is:" + score);
                                words.RemoveAt(0);
                                break;
                            }
                            
                            else
                            { 
                                score--;
                                Console.WriteLine("Wrong answer! \n \nYour current score is:" + score);
                                break;
                            }


                        case "word1":
                            if (input1 == "translation1"){
                                score++;
                                Console.WriteLine("Good job! \n \nYour current score is: {score}");
                                words.RemoveAt(1);
                                break;
                            }
                            
                            else
                            { 
                                score--;
                                Console.WriteLine("Wrong answer! \n \nYour current score is:" + score);
                                break;
                            }


                        case "word2":
                            if (input1 == "translation2"){
                                score++;
                                Console.WriteLine("Good job! \n \nYour current score is: {score}");
                                words.RemoveAt(2);
                                break;
                            }
                            
                            else
                            { 
                                score--;
                                Console.WriteLine("Wrong answer! \n \nYour current score is:" + score);
                                break;
                            }


                        case "word3":
                            if (input1 == "translation3"){
                                score++;
                                Console.WriteLine("Good job! \n \nYour current score is: {score}");
                                words.RemoveAt(3);
                                break;
                            }
                            
                            else
                            { 
                                score--;
                                Console.WriteLine("Wrong answer! \n \nYour current score is:" + score);
                                break;
                            }


                        

                    }
                    
                
                }while (input == "start" && accessKey);

                
            }
    }
}

我曾尝试使用 if... else if...效果不佳,因为我不知道如何返回并提出新问题,这就是我使用 do... while... & switch。但是,由于 printedWord 的值不断更新,因此答案最终是错误的,因为一旦我按下 Enter,打印的内容将更改为其他内容

我已经尝试在输入 input1 时创建一个布尔值并反转它...

accessKey = true
(...)
case "translation0":
                            accessKey ^= false;
                            
                            if (input1 == "translation0")
                            {
                                score++;
                                Console.WriteLine("Good job! \n \nYour current score is:" + score);
                                words.RemoveAt(0);
                                accessKey ^= true;
                                break;
                            }
                            
                            else
                            { 
                                score--;
                                Console.WriteLine("Wrong answer! \n \nYour current score is:" + score);
                                accessKey ^= true;
                                break;
                            }
}while(input == "start && accessKey);

...但这没有用

感谢任何帮助和提示,我提前感谢您。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)