2015-02-09 74 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); 

纠正所有这些行。

另外,建议先输入第一个局部变量,然后尝试转换。