2015-12-03 189 views
-7

對於此代碼,我一直收到錯誤。當我輸入「A」時,顯示「請輸入金額」,然後顯示錯誤。爲什麼我總是收到錯誤

static void Main(string[] args) 
{ 
    string SalesPerson; 
    int Sales = 0; 

    Console.WriteLine("Please enter the salesperons's initial"); 
    SalesPerson = Console.ReadLine().ToUpper(); 

    while (SalesPerson !="Z") 
    { 

     if (SalesPerson=="A") 
     { 
      Console.WriteLine("Please enter amount of a sale Andrea Made"); 
      Sales = Convert.ToInt32(SalesPerson); 
     } 

    } 
} 
+1

什麼語言的是,C#?請編輯您的問題並添加相關標籤。我懷疑這個錯誤是關於循環的,所以標籤也可能是錯誤的。你也有一些縮進問題。此外,標題對識別您的問題沒有用處,並且您沒有顯示確切的錯誤信息,也不是[mcve]。請花點時間改善您的問題! –

+0

錯誤有多大,它的癥結是什麼? –

+0

請參閱標記的副本,這是一個同樣過於寬泛的完全相同的問題。請注意,在你的代碼中,你知道'SalesPerson'的值是'「A」'。你期望'Convert.ToInt32()'將什麼_integer_值轉換爲?是什麼讓你認爲這甚至是一個有效的轉換? –

回答

1

你在混合字符串和整數。 Sales是一個int,SalesPerson是一個字符串,在你描述的情況下,是「A」。

所以,當你試試這個:

Sales = Convert.ToInt32(SalesPerson); 

...失敗,因爲「A」(營業員字符串的值)不能轉換爲整數。 「巨大」的錯誤可能基本上告訴你這一點。

-3

你可以試試這個:

static void Main(string[] args) 
    { 
     string SalesPerson; 
     int Sales = 0; 

     //The loop to ask the user for a letter 

     do 
     { 
      Console.WriteLine("Please enter the salesperons's initial"); 
      SalesPerson = Console.ReadLine().ToUpper(); 

      //If the letter is not equal to "A", repeat the prompt 

      }while (SalesPerson != "A") 

      if (SalesPerson=="A") 
      { 
       Console.WriteLine("Please enter amount of a sale Andrea Made"); 
       Sales = Convert.ToInt32(SalesPerson); 


     } 
    } 
相關問題