2013-10-24 105 views
0

我試圖創建一個C#程序,它爲用戶提供了3個選項:C#控制檯程序循環問題

  1. 創建名稱(獲取用戶輸入他們的名字和姓氏,並顯示爲J.Blogg )

  2. 若干階乘(輸出作爲例如5x4x3x2x1 = 120,其爲5

  3. 階乘退出

我有程序工作正常,但當我嘗試選擇選項1(創建名稱),然後選項2它轉到選項1,然後它不讓我退出(選項3)。 我是新來的編程,所以它可能很簡單,但我不明白我要去哪裏錯了, 任何幫助將是非常偉大的。 我想保持相同的佈局,我認爲我的問題可能是循環,但任何幫助和改進都會很好。

static void Main(string[] args) 
    { 

     //The value returned from the topmenu method is stored in a variable called useroption 
     int useroption; 
     useroption = topmenu(); 

     // excute while loop untill option is not 1-3 
     do 
     { 


      if (useroption == 1) 
      { 
       Console.Clear(); 
       Createname(); 
       //break; 
      } 

      if (useroption == 2) 
      { 
       Console.Clear(); 
       factorial(); 
       // break; 
      } 

      if (useroption == 3) 
      { 
       Console.Clear(); 
       Console.WriteLine("Thank you for using my program, Good bye !!!"); 
       // break; 
      } 

      //topmenu(); 
      } 
     while (useroption != 3); 




     Console.ReadKey(); 


    } 

    //This method present the user with an menu which the user has a choice of 3 options 
    static int topmenu() 
    { 
     int option; 
     string option_str; 

     Console.Clear(); 
     Console.WriteLine("********************************************************************************"); 
     Console.WriteLine("********************************************************************************"); 
     Console.WriteLine("*********  OPTION 1 : Enter your name        *********"); 
     Console.WriteLine("*********  OPTION 2 : Enter the number you want to factorise  *********"); 
     Console.WriteLine("*********  OPTION 3 : Quit           *********"); 
     Console.WriteLine("********************************************************************************"); 
     Console.WriteLine("********************************************************************************"); 
     option_str = Console.ReadLine(); 

     option = Convert.ToInt32(option_str); 
     Console.Clear(); 

     if (option < 0 || option > 3) 
     { 
      Console.WriteLine("You have enter an invald option,"); 
      Console.WriteLine("Please chose a option between 1-3 (Please press any key to return to main menu)"); 
      Console.ReadLine(); 
      Console.Clear(); 
      topmenu(); 
     } 
     else 
     { 
      Console.WriteLine("You have chosen option: " + option + " (Please press any key continue)"); 
     } 
     Console.ReadKey(); 
     return option; 




    } 
    //this method asks user to enter their name (1st name then surname) and presents it back to the user as their intial(1st name) and surname 
    static void Createname() 
    { 
     string firstname, surname, firstname_str, surname_str, userfullname; 

     Console.Clear(); 
     Console.WriteLine("Please enter your first name "); 
     firstname_str = Console.ReadLine(); 
     firstname = Convert.ToString(firstname_str); 
     Console.Clear(); 
     Console.WriteLine("Please enter your surname name "); 
     surname_str = Console.ReadLine(); 
     surname = Convert.ToString(surname_str); 
     Console.Clear(); 
     userfullname = firstname + surname; 

     Console.WriteLine("You have entered your name as " + firstname[0] + "." + surname); 
     Console.WriteLine("(Please press any key to return to main menu)"); 
     Console.ReadKey(); 
     topmenu(); 

    } 

    //this method asks the user to enter a number and returns the factorial of that number 
    static double factorial() 
    { 


     string number_str; 
     double factorial = 1; 



     Console.WriteLine("Please enter number"); 
     number_str = Console.ReadLine(); 

     int num = Convert.ToInt32(number_str); 


     // If statement is used so when the user inputs 0, INVALID is outputed 

     if (num <= 0) 
     { 
      Console.WriteLine("You have enter an invald option"); 
      Console.WriteLine("Please enter number"); 
      number_str = Console.ReadLine(); 
      Console.Clear(); 


      num = Convert.ToInt32(number_str); 
      //Console.Clear(); 
      //topmenu(); 
      //number_str = Console.ReadLine(); 
     } 

     if (num >= 0) 
     { 

      while (num != 0) 
      { 
       for (int i = num; i >= 1; i--) 
       { 
        factorial = factorial * i; 
        Console.Write(i + " * "); 

       } 


        Console.WriteLine("= "+factorial+ " which is factorial of " + number_str.ToString()); 
        Console.WriteLine("(please any key to return to main menu)"); 
        Console.ReadKey(); 
        Console.Clear(); 
        topmenu(); 

      } 

     } 


     return factorial; 




    } 
} 

}

+1

請儘量減小樣本(查看http://www.sscce.org獲取指導)。對於未來的帖子,儘量避免「謝謝你的提示」/「新到這裏」以及其他與你的問題沒有直接關係的文本(尤其是佔用一半的帖子)。 –

回答

3

只要把這些線內做......而

int useroption; 
useroption = topmenu(); 

重新排列如下...

int useroption; 
     // excute while loop untill option is not 1-3 
     do 
     { 

      useroption = topmenu(); 

和你的程序將正常工作

完整的代碼是在這裏: http://pastebin.com/fCh0ttUY

+0

謝謝..當它循環回選項菜單時仍然有問題。 – user2916941

+0

問題是什麼? – gypsyCoder

+0

一旦它循環回來,如果我嘗試選項2,它會讓我在繼續使用階乘之前多次按下輸入,如果使用選項3之後(QUIT),它會繼續顯示階乘,但每次按Enter後都會持續增加而不是Quiting .. – user2916941

0

人首先,設置useroption到0執行一些代碼後。否則它將繼續執行它。其次,ReadKey()就在while語句之前。否則,你將無法讀取輸入。

+0

請在代碼中提供一個示例。 – Brian

+0

謝謝你的幫助!它看起來表現得好一點,但仍然不能正常工作,就像你說的那樣,但是一旦它回到主菜單,它需要用戶按幾次輸入才能確定爲什麼。 – user2916941

+0

然後當我嘗試Option3(QUIT)時,它會一直顯示階乘結果,並在每次嘗試時增加它。 – user2916941

0

問題是,儘管您再次顯示topmenu y你永遠不要重新分配有用的價值。

正如gypsyCoder所說,在do {} while()塊內移動菜單的顯示將解決您的問題,因爲它會導致循環中每次循環重新分配用途。

+0

謝謝..一旦我移動並在Do循環中添加以下行,仍然不能正確工作... useroption = 0; useroption = topmenu(); Console.ReadKey(); – user2916941

+0

你必須要更具描述性。 「仍然無法工作」是沒有足夠的信息來幫助解決您的問題。 – BenCr