C# 控制台 case 语句

发布于 2024-08-15 07:29:58 字数 3590 浏览 3 评论 0原文

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace LittleQuizworld_3
{
    class Program
    {
    public string QuestionText; //Actual question text.
    public string[] Choices; //Array of answer from which user can choose.
    public int Answer; //Index of correct answer with Choices.



    static void Main(string[] args)

    {

        int correct = 0;
        int iChoice =0;

        Console.WriteLine("Hello Welcome to Litte Quiz World, We hope you will have fun here");
        Console.WriteLine("Please press enter to continues :) ");
        Console.ReadLine();
        while (iChoice != 4)
        {
        Console.WriteLine("Please select the following");
        Console.WriteLine("1.Play Game");
        Console.WriteLine("2.Future game");
        Console.WriteLine("3.Credits");
        Console.WriteLine("4.Exit");
        Console.ReadLine();
        iChoice = Convert.ToInt32(Console.ReadLine());



          using (StreamReader sr = new StreamReader("./quiz.txt"))
        {



           switch (iChoice)
           {

         case 1:

            while (!sr.EndOfStream)
            {
                Console.Clear();
                for (int i = 0; i < 5; i++)
                {
                    String line = sr.ReadLine();
                    if (i > 0)
                    {
                        if (line.Substring(0, 1) == "#") correct = i;
                        Console.WriteLine("{0}: {1}", i, line);
                    }
                    else
                    {
                        Console.WriteLine(line);
                    }
                }

                for (;;){
                {
                    Console.Write("Select Answer: ");
                    ConsoleKeyInfo cki = Console.ReadKey();
                    if (cki.KeyChar.ToString() == correct.ToString())
                    {
                        Console.WriteLine(" - Correct!");
                        Console.WriteLine("Press any key for next question...");
                        Console.ReadKey();

                    }
                    else
                    {
                        Console.WriteLine(" - Try again!");
                        Console.Clear();
                    }




                    case 2:

                    if (iChoice == 2)
                    {
                        Console.WriteLine("Future game of Little Quiz World");
                        Console.WriteLine("Little Quiz Would will continues working on the patch for this game");
                        Console.WriteLine("Also the new game which is planned will be call BattleShip World beta testing will be very soon ");
                        Console.WriteLine("Please stick close to us ");
                        Console.ReadLine();
                    }
                            break;
                    case 3:
                    if (iChoice == 3)
                    {
                        Console.WriteLine("Credit");
                        Console.WriteLine("We hope you enjoy the game and please feel free to give us feeback at email : [email protected]");
                        Console.ReadLine();
                    }
                    break;


                        }
                    }
                }
             }
            }
        }

    }
}

} 我创建了一个小测验游戏和一个小菜单系统,但我的案例陈述似乎不起作用,有人可以帮我吗?

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace LittleQuizworld_3
{
    class Program
    {
    public string QuestionText; //Actual question text.
    public string[] Choices; //Array of answer from which user can choose.
    public int Answer; //Index of correct answer with Choices.



    static void Main(string[] args)

    {

        int correct = 0;
        int iChoice =0;

        Console.WriteLine("Hello Welcome to Litte Quiz World, We hope you will have fun here");
        Console.WriteLine("Please press enter to continues :) ");
        Console.ReadLine();
        while (iChoice != 4)
        {
        Console.WriteLine("Please select the following");
        Console.WriteLine("1.Play Game");
        Console.WriteLine("2.Future game");
        Console.WriteLine("3.Credits");
        Console.WriteLine("4.Exit");
        Console.ReadLine();
        iChoice = Convert.ToInt32(Console.ReadLine());



          using (StreamReader sr = new StreamReader("./quiz.txt"))
        {



           switch (iChoice)
           {

         case 1:

            while (!sr.EndOfStream)
            {
                Console.Clear();
                for (int i = 0; i < 5; i++)
                {
                    String line = sr.ReadLine();
                    if (i > 0)
                    {
                        if (line.Substring(0, 1) == "#") correct = i;
                        Console.WriteLine("{0}: {1}", i, line);
                    }
                    else
                    {
                        Console.WriteLine(line);
                    }
                }

                for (;;){
                {
                    Console.Write("Select Answer: ");
                    ConsoleKeyInfo cki = Console.ReadKey();
                    if (cki.KeyChar.ToString() == correct.ToString())
                    {
                        Console.WriteLine(" - Correct!");
                        Console.WriteLine("Press any key for next question...");
                        Console.ReadKey();

                    }
                    else
                    {
                        Console.WriteLine(" - Try again!");
                        Console.Clear();
                    }




                    case 2:

                    if (iChoice == 2)
                    {
                        Console.WriteLine("Future game of Little Quiz World");
                        Console.WriteLine("Little Quiz Would will continues working on the patch for this game");
                        Console.WriteLine("Also the new game which is planned will be call BattleShip World beta testing will be very soon ");
                        Console.WriteLine("Please stick close to us ");
                        Console.ReadLine();
                    }
                            break;
                    case 3:
                    if (iChoice == 3)
                    {
                        Console.WriteLine("Credit");
                        Console.WriteLine("We hope you enjoy the game and please feel free to give us feeback at email : [email protected]");
                        Console.ReadLine();
                    }
                    break;


                        }
                    }
                }
             }
            }
        }

    }
}

}
I create a little quiz game and a little menu system but my case statment not seem to work , can anyone give me a hand here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯然 2024-08-22 07:29:58

撇开大括号的平衡不谈,请注意,在 case 2: 下使用 if (iChoice==2) 是多余的,因为您的 switch 语句位于 iChoiceiChoice==3 也是如此。

编辑:
为了澄清起见,您应该写

case 2:
    if (iChoice == 2)
    {
        //...
    }

case 2:
    //...

但这并不是阻止代码编译的原因。正如其他人指出的那样,您的大括号不平衡,并且您将一些 case 语句放入循环中,而其他语句则没有。

Balancing of braces aside, note that it is redundant to have if (iChoice==2) under case 2: because your switch statement is on iChoice. Same goes for iChoice==3.

Edit:
To clarify, you should write

case 2:
    if (iChoice == 2)
    {
        //...
    }

as

case 2:
    //...

But this isn't what's stopping your code from compiling. As others have pointed out, your braces are not balanced and you are putting some of your case statements inside a loop while others are not.

转角预定愛 2024-08-22 07:29:58

检查你的牙套。它们不平衡——至少不是你想要的那样。

Check your braces. They're not balanced - at least, not the way you want them to be.

没︽人懂的悲伤 2024-08-22 07:29:58

哇,你在一个函数中发生了很多事情。如果您将其分成多个函数,并从每个 case 语句中调用每个函数,那么跟踪问题可能会更容易。

此外,您还有看起来跳到循环中间的 case 语句。

Wow, you have a lot going on there in one function. It might be easier to track down issues if you were to separate this into several functions, call each function from each case statement.

Also you have case statements that appear to jump into the middle of loops.

记忆之渊 2024-08-22 07:29:58

撇开大括号的平衡不谈,请注意,在情况 2: 下使用 if (iChoice==2) 是多余的,因为您的 switch 语句位于 iChoice 上。 iChoice==3 也是如此。

抱歉,我不太明白,我是 C# 新手

Balancing of braces aside, note that it is redundant to have if (iChoice==2) under case 2: because your switch statement is on iChoice. Same goes for iChoice==3.

sorry i dont really get it , i am a newbie in c#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文