【发布时间】:2015-04-08 21:51:22
【问题描述】:
我正在尝试制作一个简单的兴趣计算器,其中一个人输入一个数字 (1 - 4) 来表示他们想要计算的内容,然后输入给定的数字并获取缺失的变量。
代码:
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'
【问题讨论】:
-
在您的代码中,在每种情况下都更改:initial = Convert.ToInt32(Console.ReadLine);初始 = Convert.ToInt32(Console.ReadLine());对“时间”做同样的事情
-
我还建议您不要使用“final”这个词作为变量的名称,这只是为了良好的做法(因为它是一个关键字)。最后,永远不要直接尝试将用户的输入转换为整数。如果您填写非数字值,应用程序将崩溃。
-
@Tarske 虽然我同意你关于不使用关键字作为标识符的观点,但我认为在这里使用 final 没有任何问题,因为它不是 C# 中的关键字
-
@ThomasSchremser 哎呀,你是对的。抱歉,感谢您的更正。
标签: c# compiler-errors switch-statement calc