前两个参数似乎有效,但是当我添加字符串时出现错误(第17行无法将字符串转换为double)。我在这里错过了什么?从我在书中看到的所有内容来看,这似乎应该有效,所以我猜这是一个愚蠢的错误,但我一直在查看最近3个小时的代码并且没有找到任何东西。感谢您阅读此内容!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
SimpleCalc Calc = new SimpleCalc("{0.0}", "{0.0}", "{0}");
Console.WriteLine(Calc);
}
}
}
这是班级
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication14
{
class SimpleCalc
{
public SimpleCalc(double num1, double num2, string oper)
{
Console.Write("Enter first integer: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter operator (+,-,*, / or %)");
oper = Convert.ToString(Console.ReadLine());
Console.Write("Enter second integer: ");
num2 = Convert.ToDouble(Console.ReadLine());
if (oper == "+")
Console.Write("Answer is: {0}", num1 + num2);
if (oper == "-")
Console.Write("Answer is: {0}", num1 - num2);
if (oper == "*")
Console.Write("Answer is: {0}", num1 * num2);
if (oper == "/")
Console.Write("Answer is: {0}", num1 / num2);
if (oper == "%")
Console.Write("Answer is: {0}", num1 % num2);
Console.ReadKey();
}
}
}
答案 0 :(得分:9)
问题是你的构造函数需要两个双精度数和一个字符串:
public SimpleCalc(double num1, double num2, string oper)
但是你用三个字符串来调用它:
SimpleCalc Calc = new SimpleCalc("{0.0}", "{0.0}", "{0}");
将其更改为:
SimpleCalc calc = new SimpleCalc(0.0, 0.0, "{0}");
在编译方面应该没问题。 (在构造函数中与用户交互并忽略传入的参数值并不好,但这是另一回事。)
另请注意,我已将局部变量的名称从Calc
更改为calc
,以遵循正常的C#约定。
答案 1 :(得分:3)
您的SimpleCalc
构造函数需要double
和最后一个string
..您只传递字符串。
所以,而不是:
SimpleCalc Calc = new SimpleCalc("{0.0}", "{0.0}", "{0}");
你应该这样做:
SimpleCalc Calc = new SimpleCalc(0.0, 0.0, "0");
答案 2 :(得分:0)
首先,你是一些转换错误字符串加倍并使用无效的args重载。
其次,您是将参数传递给构造函数但不使用它们?
也许你想要
static void Main(string[] args)
{
SimpleCalc Calc = new SimpleCalc();
Console.WriteLine(Calc);
}
和consructor
public SimpleCalc()
{
double num1;
double num2;
string oper;
Console.Write("Enter first integer: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter operator (+,-,*, / or %)");
oper = Convert.ToString(Console.ReadLine());
Console.Write("Enter second integer: ");
num2 = Convert.ToDouble(Console.ReadLine());
if (oper == "+")
Console.Write("Answer is: {0}", num1 + num2);
if (oper == "-")
Console.Write("Answer is: {0}", num1 - num2);
if (oper == "*")
Console.Write("Answer is: {0}", num1 * num2);
if (oper == "/")
Console.Write("Answer is: {0}", num1 / num2);
if (oper == "%")
Console.Write("Answer is: {0}", num1 % num2);
Console.ReadKey();
}
答案 3 :(得分:0)
您的代码有多个错误。如果您从parameters
函数发送Main
,则从中获取此参数。如果您从Main
函数发送参数,为什么要在SimpleCalc
函数的控制台中读取它。
首先将阅读部分从SimpleCalc
功能移至Main
。
static void Main(string[] args)
{
double num1;
double num2;
string oper;
Console.Write("Enter first integer: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter operator (+,-,*, / or %)");
oper = Convert.ToString(Console.ReadLine());
Console.Write("Enter second integer: ");
num2 = Convert.ToDouble(Console.ReadLine());
SimpleCalc Calc = new SimpleCalc(num1,num2,oper);
Console.WriteLine(Calc);
}
并从功能中删除阅读部分并使其简单明了
public SimpleCalc(double num1, double num2, string oper)
{
if (oper == "+")
Console.Write("Answer is: {0}", num1 + num2);
if (oper == "-")
Console.Write("Answer is: {0}", num1 - num2);
if (oper == "*")
Console.Write("Answer is: {0}", num1 * num2);
if (oper == "/")
Console.Write("Answer is: {0}", num1 / num2);
if (oper == "%")
Console.Write("Answer is: {0}", num1 % num2);
Console.ReadKey();
}
答案 4 :(得分:0)
您需要传递在方法中启动的正确类型的参数。请参阅下面的代码。
SimpleCalc Calc = new SimpleCalc(0.0,0.0," 0");
或
你可以通过以下方式。
SimpleCalc Calc = new SimpleCalc(" {0.0}"," {0.0}"," {0}");
但是你需要按照下面的要求创建方法
class SimpleCalc
{
public SimpleCalc(string num1, string num2, string oper)
{
//............
}
}
答案 5 :(得分:0)
由于每个人都已经指出了传递给类构造函数的参数类型的问题,我希望在代码中指出一个逻辑问题
传递给SimpleCalc构造函数的参数在构造函数中变为局部变量,并且根本不使用,因为您要求用户键入它们的值。那么为什么要在第一时间传递它们呢?我也发现不是一个很好的例子,你的类的所有工作都是在构造函数中执行的(在没有任何验证的情况下也是输入)。
也许你应该使用不同的方法
static void Main(string[] args)
{
// To keep short, I have not added any validation, but the user input
// should always validated See double.TryParse
Console.Write("Enter first integer: ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter operator (+,-,*, / or %)");
string oper = Convert.ToString(Console.ReadLine());
Console.Write("Enter second integer: ");
double num2 = Convert.ToDouble(Console.ReadLine());
SimpleCalc calc = new SimpleCalc(num1, num2, oper);
double result = calc.Execute();
Console.WriteLine("Answer is: {0}", result);
}
class SimpleCalc
{
double _num1;
double _num2;
string _oper;
public SimpleCalc(double num1, double num2, string oper)
{
_num1 = num1;
_oper = oper;
_num2 = num2;
}
public double Execute()
{
if (oper == "+")
return num1 + num2;
else if (oper == "-")
return num1 - num2;
else if (oper == "*")
return num1 * num2;
else if (oper == "/")
return num1 / num2;
else if (oper == "%")
return num1 % num2;
else
throw new ArgumentException("Invalid operator: " + oper);
}
}
答案 6 :(得分:-1)
您应该如下使用它 SimpleCalc Calc = new SimpleCalc(0.0,0.0,“{0}”);
因为你的班级要求前两个参数为双精度,第三个参数为字符串