C#控制台case语句
问题描述:
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;
}
}
}
}
}
}
}
}
} 我创建了一个小测试游戏和一个小菜单系统,但我的情况似乎不statment工作,任何人都可以给我一只手在这里吗?C#控制台case语句
答
检查你的大括号。他们不平衡 - 至少,不是你想要他们的方式。
答
哇,你有很多事情在一个功能。如果要将它分成几个函数,可以更容易地追踪问题,从每个case语句调用每个函数。
此外,您还有似乎跳入循环中间的case语句。
答
不考虑大括号的平衡,请注意if (iChoice==2)
在case 2:
之下是多余的,因为您的switch语句在iChoice
。 iChoice==3
也一样。
编辑: 为了澄清,你应该写
case 2:
if (iChoice == 2)
{
//...
}
为
case 2:
//...
但这不是什么从停止编译代码。正如其他人所指出的那样,你的大括号不平衡,你正在把一些你的案例陈述放在一个循环中,而另一些则不是。
答
将支架平衡放在一边,注意在情况2下if(iChoice == 2)是多余的:因为您的switch语句位于iChoice上。 iChoice == 3也一样。
对不起,我真的不明白,我是一个新手在c#
我的头痛从刚刚尝试阅读此。你能正确地格式化你的代码吗(即严格遵守每个嵌套'{}')内容的缩进级别?如果你这样做,我强烈怀疑你会自己看到问题。事实上,你有这样一个奇怪的大括号组合,它甚至不应该编译,也不清楚它要编译时应该做什么。 – 2009-12-17 04:45:49