2016-11-27 145 views
-2

我是新來的編碼,我試圖從一些問題開始討論。它一直很好,直到我到底,它給了我一些關於無效令牌的錯誤。請閱讀這些行,並告訴我什麼是錯許多無效的令牌錯誤C#

using System; 

namespace CheckIfOver18 
{ 
    class MainClass 
    { 
     public static void Main(string[] args) 
     { 
     Start: 
      Console.WriteLine("How old are you? \n"); 
      int age = Convert.ToInt32(Console.ReadLine()); 
      int yes = y; 
      if (age < 18) 
      { 
       Console.WriteLine("This game contains dismemberment and violence between victional characters, please get your parents or guardians to answer the next question."); 
      } 
      Console.WriteLine("\n This game contains dismemberment and violence between victional characters, do you allow your child to play this game? Please type y for yes or n for no"); 
      Console.ReadKey(yes); 
      Console.WriteLine("Thank you for letting your child play this game, <press any key to continue"); 
      Console.ReadKey(); 
     } 
      else Console.WriteLine ("Sorry your child is not able to play this game"); 
     goto Start; 
      if (age > 18){ 
       Console.WriteLine("<Press any key to continue"); 
       Console.ReadKey(); 
     } 
    } 
} 

林新,所以如果你能告訴我該怎麼做直接的話,這將是很好的

+1

你是什麼意思'int yes = y;'? y是什麼?你也確定你真的想使用'goto'嗎? – meJustAndrew

+0

我試圖讓他們鍵入y是或n沒有 – Chody

+0

在C#中沒有鍵入y, – meJustAndrew

回答

1

這部分

else Console.WriteLine ("Sorry your child is not able to play this game"); 
    goto Start; //maybe without this line 
     if (age > 18){ 
      Console.WriteLine("<Press any key to continue"); 
      Console.ReadKey(); 
    } 

不應該現在它在else以上的一條線上,從Main方法有一個右括號,所以這段代碼在它之外,它不能在那裏。也許這應該是檢查<年齡18

第一條件也有與yes = y問題,通過yesConsole.ReadKey不帶任何參數之後。

您可以輕鬆地重寫此代碼以擺脫goto,因爲在這種簡單的情況下不需要它。