2015-02-09 61 views
0

我想要做一個簡單的興趣計算器,其中一個人輸入一個數字(1 - 4)他們想要計算,然後輸入給定的數字,並得到缺少的變量。C#簡單的利息計算器開關錯誤

代碼:

using System; 
using System.Convert; 

public class InterestCalculator { 
    static public void Main(string [] args) { 
     int final, initial, rate, time, input; 

     Console.WriteLine("What do you want to calculate? \n 1. Final amount after interest. \n 2. Initial amount after interest. \n 3. Interest rate. \n 4. Time passed"); 
     input = Convert.ToInt32(Console.ReadLine()); 
     switch (input){ 
      case 1: 
       Console.WriteLine("Enter the initial amount."); 
       initial = Convert.ToInt32(Console.ReadLine()); 
       Console.WriteLine("Enter the interest rate."); 
       rate = Convert.ToInt32(Console.ReadLine()); 
       Console.WriteLine("Enter the time passed."); 
       time = Convert.ToInt32(Console.ReadLine()); 
       final = initial * rate * time; 
       Console.WriteLine("$" + final + " is the final amount after interest."); 
       break; 
      case 2: 
       Console.WriteLine("Enter the final amount."); 
       final = Convert.ToInt32(Console.ReadLine()); 
       Console.WriteLine("Enter the interest rate."); 
       rate = Convert.ToInt32(Console.ReadLine()); 
       Console.WriteLine("Enter the time passed."); 
       time = Convert.ToInt32(Console.ReadLine); 
       initial = final/(rate * time); 
       Console.WriteLine("$" + initial + " is the initial amount before interest."); 
       break; 
      case 3: 
       Console.WriteLine("Enter the final amount."); 
       final = Convert.ToInt32(Console.ReadLine()); 
       Console.WriteLine("Enter the initial amount."); 
       initial = Convert.ToInt32(Console.ReadLine()); 
       Console.WriteLine("Enter the time passed."); 
       time = Convert.ToInt32(Console.ReadLine); 
       rate = final/(initial * time); 
       Console.WriteLine("%" + initial + " per time cycle is the interest rate"); 
       break; 
      case 4: 
       Console.WriteLine("Enter the final amount."); 
       final = Convert.ToInt32(Console.ReadLine()); 
       Console.WriteLine("Enter the initial amount."); 
       initial = Convert.ToInt32(Console.ReadLine); 
       Console.WriteLine("Enter the interest rate."); 
       rate = Convert.ToInt32(Console.ReadLine()); 
       time = final/(initial * rate); 
       Console.WriteLine(initial + " cycles is the amount of time passed."); 
       break;} 
      default: 
       Console.WriteLine("Invalid input."); 
     } 
    } 
} 

我不斷(使用單聲道)得到這個錯誤在編譯過程:

error CS1502: The best overloaded method match for System.Convert.ToInt32(bool) has some invalid arguments 
error CS1503: Argument `#1' cannot convert `method group' expression to type `bool' 
error CS1502: The best overloaded method match for `System.Convert.ToInt32(bool)' has some invalid arguments 
error CS1503: Argument `#1' cannot convert `method group' expression to type `bool' 
error CS1502: The best overloaded method match for `System.Convert.ToInt32(bool)' has some invalid arguments 
error CS1503: Argument `#1' cannot convert `method group' expression to type `bool' 
+1

在你的代碼中,在任何情況下都會改變: initial = Convert.ToInt32(Console.ReadLine); to initial = Convert.ToInt32(Console.ReadLine()); 做同樣的「時間」 – 2015-02-09 07:47:36

+1

我還建議你不要使用'final'這個詞作爲變量的名稱,只是爲了好的做法(因爲它是一個關鍵字)。 最後,不要直接嘗試將用戶的輸入轉換爲整數。如果您填寫非數字值,該應用將崩潰。 – Tarske 2015-02-09 07:50:14

+1

@Tarske雖然我同意你關於不使用關鍵字作爲標識符我看到在這裏使用final沒有錯,因爲它不是C#中的關鍵字 – 2015-02-09 09:54:29

回答

4

那麼一個你Console.ReadLine()沒有括號。所以你傳遞的方法而不是調用它。

+2

到目前爲止,我已經計算了三次。 – 2015-02-09 07:50:41

+0

哦,我現在看到了,謝謝! – Zunon 2015-02-09 07:56:31

0

大部分的地方你只有

到Console.ReadLine而不是到Console.ReadLine()

入住代碼。例如 -

案例2

time = Convert.ToInt32(Console.ReadLine); 

糾正所有這些行。

另外,建議先輸入第一個局部變量,然後嘗試轉換。