2009-12-17 109 views
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; 


         } 
        } 
       } 
      } 
      } 
     } 

    } 
} 

} 我創建了一個小測試遊戲和一個小菜單系統,但我的情況似乎不statment工作,任何人都可以給我一隻手在這裏嗎?C#控制檯case語句

+0

我的頭痛從剛剛嘗試閱讀此。你能正確地格式化你的代碼嗎(即嚴格遵守每個嵌套'{}')內容的縮進級別?如果你這樣做,我強烈懷疑你會自己看到問題。事實上,你有這樣一個奇怪的大括號組合,它甚至不應該編譯,也不清楚它要編譯時應該做什麼。 – 2009-12-17 04:45:49

回答

0

檢查你的大括號。他們不平衡 - 至少,不是你想要他們的方式。

0

哇,你有很多事情在一個功能。如果要將它分成幾個函數,可以更容易地追蹤問題,從每個case語句調用每個函數。

此外,您還有似乎跳入循環中間的case語句。

0

不考慮大括號的平衡,請注意if (iChoice==2)case 2:之下是多餘的,因爲您的switch語句在iChoiceiChoice==3也一樣。

編輯: 爲了澄清,你應該寫

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

case 2: 
    //... 

但這不是什麼從停止編譯代碼。正如其他人所指出的那樣,你的大括號不平衡,你正在把一些你的案例陳述放在一個循環中,而另一些則不是。

0

將支架平衡放在一邊,注意在情況2下if(iChoice == 2)是多餘的:因爲您的switch語句位於iChoice上。 iChoice == 3也一樣。

對不起,我真的不明白,我是一個新手在c#