使用"case"(案例)运行更多代码 [英] Using "case" to run more code

查看:128
本文介绍了使用"case"(案例)运行更多代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我试图创建一个简单的控制台程序,问一个问题.如果回答正确,它将运行更多代码,依此类推.我的代码:

 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 ConsoleApplication2
{
     class 程序
    {
        静态 无效 Main(字符串 []参数)
        {
            // Spørgsmål1张剧照
            Console.WriteLine(" );
            //  Spilleren angiver et svar,som bliver lagret som"input" 
            字符串 input = Console.ReadLine();
            开关(input.ToLower())
            {
                //  Hvis Splasherensgæt(input)var"bil",bliver denne kodekørt
                案例 " :
                    Console.WriteLine(" );
                    Console.WriteLine();

                    // Spørgsmål2张剧照
                    Console.WriteLine(" );
                    字符串 input2 = Console.ReadLine();
                    开关(input.ToLower())
                    {
                        案例 " :
                            Console.WriteLine(" );
                            Console.WriteLine();
                             break ;
                        默认:
                            Console.Write(" );
                             break ;
                    }
                     break ;
                //  Hvis漏斗获得(输入)IKKE var"bil",bliver denne koekørt
                默认:
                    Console.Write(" );
                     break ;
            }
            Console.ReadKey();
        }
    }
} 



如果您尝试运行代码,您将看到第二个问题运行不正确.它一直使用默认大小写..我也不知道为什么:(

我希望有人能看到问题:)

解决方案

替换

 字符串 input2 = Console.ReadLine();
开关(input.ToLower())



 字符串 input2 = Console.ReadLine();
开关(input2.ToLower()) 


首先,您应将此答案标记为已完成,因为André已回答克拉克.

然后,您应该打开第二个线程.

第二个问题的答案是使用for循环和Random生成器.
有很多方法可以做到这一点.这是一个.

将您的问题和正确答案放在KeyValuePairs列表中.它应该是一个列表,以便对其进行索引.然后使用随机值生成器,您可以选择哪个问题并将其删除(这样就不会再使用它了.).

像这样的东西:

  var  qAndACollection = GetQuestionsAndAnswers(); //  var  randObject =  Random();

 for ( int  q =  1 ; q <  = Q_COUNT; q ++)
{
     // 通过随机选择索引来随机获取问题解答
      var  randIndex = randObject.Next( 0 ,qAndACollection.Count- 1 );

    // 获取常见问题解答
     var  qAndA = qAndACollection [randIndex];

    // 将其删除,以免再次使用
    qAndACollection.RemoveAt(randIndex);

    // 使用问题与解答"提示用户
     var  correctAnswer = PromptUser(qAndA);

    // 如果用户不正确,请提前退出
    如果(!correctAnser)
        break ;
}

...

私有 无效 PromptUser(KeyValuePair [字符串字符串] qAndA)
{
   // 提示输入q并检查用户是否回答A并返回
} 



嗯,我的代码没有显示. hoo我会再试一次.


您的代码中最糟糕的事情是使用立即常量,尤其是字符串,尤其是作为大小写选择器.您如何支持他们?如果您拼写错误的字符串文字,编译器将不会告诉您任何信息.使用enum是另一回事,它是完全安全的,并且已由编译器确认.

有关使用enum的一些相关技术,请参阅我的文章``枚举类型不枚举!''解决.NET和语言限制 .

关于长案陈述,我发明了一种有力的补救措施,这可能对您来说很有趣.无论如何,很多人都喜欢.
请参阅我的文章动态方法分派器",不再有长开关语句! .

—SA


Hi!

Im trying to create a simple console program, that asks a question. If you answer correct, it runs a more code, and so on. My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Spørgsmål 1 stilles
            Console.WriteLine("Hvad har fire hjul, og køre på vejen?");
            //Spilleren angiver et svar, som bliver lagret som "input"
            string input = Console.ReadLine();
            switch (input.ToLower())
            {
                //Hvis spillerens gæt (input) var "bil", bliver denne kode kørt
                case "bil":
                    Console.WriteLine("Det er rigtigt!");
                    Console.WriteLine();

                    //Spørgsmål 2 stilles
                    Console.WriteLine("Spørgsmål 2");
                    string input2 = Console.ReadLine();
                    switch (input.ToLower())
                    {
                        case "gul":
                            Console.WriteLine("Det er rigtigt!");
                            Console.WriteLine();
                            break;
                        default:
                            Console.Write("Det er forkert!");
                            break;
                    }
                    break;
                //Hvis spilleres get (input) IKKE var "bil", bliver denne koe kørt
                default:
                    Console.Write("Det er forkert!");
                    break;
            }
            Console.ReadKey();
        }
    }
}



If you try to run the code, you will see that the second question isnt running correct. It uses the default case all the time.. And i have no clue why :(

I hope someone can see the problem :)

解决方案

Replace

string input2 = Console.ReadLine();
switch (input.ToLower())


with

string input2 = Console.ReadLine();
switch (input2.ToLower())


First you should mark this answer complete as it was answered by André Kraak.

You should then have opened a second thread.

The answer to your second question is to use a for loop and the Random generator.
There are many ways to do this. Here is one.

Place your questions and correct answers in a list of KeyValuePairs. It should be a list so that it is indexed. Then using a random value genrator you can pick which question and remove it (so it does not get used again).

Something like this:

var qAndACollection = GetQuestionsAndAnswers();//Just a filler method that returns a list of KVPs (q and a)

var randObject = new Random();

for(int q = 1; q <= Q_COUNT; q++)
{
     //Get the q&a randomly by picking an index randomly
     var randIndex = randObject.Next(0, qAndACollection.Count - 1);

    //Get the Q&A
    var qAndA = qAndACollection[randIndex];

    //Remove it so it is not used again
    qAndACollection.RemoveAt(randIndex);

    //Prompt the user using the Q&A
    var correctAnswer = PromptUser(qAndA); 

    //Exit early if the user was incorrect
    if(!correctAnser)
       break;   
}

...

private void PromptUser(KeyValuePair[string, string] qAndA)
{
   //Prompt with the q and check if the user ansers with A and return
}



[EDIT] Hmmm My code did not show up. Phooi. I will try again.


Worst thing in your code is using immediate constants, especially strings, especially as case selectors. How can you support them? A compiler won''t tell you anything if you misspell a string literal. Using enum is a different story, it''s perfectly safe and confirmed by the compiler.

For some relevant techniques of using enum, see my article ''Enumeration Types do not Enumerate!'' Working around .NET and Language Limitations.

As to long case statement, I''ve invented a powerful remedy which might be interesting for you. Anyway, many like it.
Please see my article ''Dynamic Method Dispatcher'', No more long switch statements!.

—SA


这篇关于使用"case"(案例)运行更多代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆