C# 在计算器的方法之间传递变量时出现CS7036错误(来自我在互联网上找到的代码测试)
CS7036 C#未给出与“Calculator.Add(int,int,int)”的必需形式参数“num1”相对应的参数。减法、除法和乘法的错误相同 测试规则规定,每个操作必须有单独的方法 在经历了不可避免的长时间休息后,试图重新开始编码。思维测试将是再次激发代码大脑的好方法 反正 我尝试过不同的方法参数,比如将变量作为引用和直接值传递,调用方法的不同方式,不同的安全参数(私有、公共、静态等) 我花了2个小时搜索不同的论坛和脚本引用,看看有没有什么,但到目前为止没有运气。测试规则规定,每个操作必须有单独的方法 我之前有一个错误,关于num1 2和op在没有标识符的情况下也被传递,所以这可能是原因之一 我知道这不是制作计算器最有效的方法,但是,这是测试想要的方式C# 在计算器的方法之间传递变量时出现CS7036错误(来自我在互联网上找到的代码测试),c#,.net,methods,console,calculator,C#,.net,Methods,Console,Calculator,CS7036 C#未给出与“Calculator.Add(int,int,int)”的必需形式参数“num1”相对应的参数。减法、除法和乘法的错误相同 测试规则规定,每个操作必须有单独的方法 在经历了不可避免的长时间休息后,试图重新开始编码。思维测试将是再次激发代码大脑的好方法 反正 我尝试过不同的方法参数,比如将变量作为引用和直接值传递,调用方法的不同方式,不同的安全参数(私有、公共、静态等) 我花了2个小时搜索不同的论坛和脚本引用,看看有没有什么,但到目前为止没有运气。测试规则规定,每个操作
using System;
namespace code_challenge.Challenges
{
public class Calculator
{
/*
* Within this Calculator class you will need to create 4 methods.
* The four methods will relate to the basic functions of a calculator
and should be named:
*
* - Add
* - Subtract
* - Multiply
* - Divide
int num1 =0; int num2 =0; int ans =0; string op = " ";
public void Input(int num1,int num2,int ans,string op)
{
Console.WriteLine("Please enter the first number.");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the opperand.");
op = Console.ReadLine();
Console.WriteLine("Please enter the Second number.");
num2 = Convert.ToInt32(Console.ReadLine());
if (op == "+")
{
ERROR LINE Add();
}
if (op == "-")
{
ERROR LINE Sub();
}
if (op == "*")
{
ERROR LINE Mul();
}
if (op == "/")
{
ERROR LINE Div();
}
}
// Implement the add function below here
public void Add(int num1, int num2, int ans)
{
ans = num1 + num2;
Console.WriteLine("Your Answer is: " + ans);
}
// Implement the subtract function below here
public void Sub(int num1,int num2,int ans)
{
ans = num1 - num2;
Console.WriteLine("Your Answer is: " + ans);
}
// Implement the multiply function below here
public void Mul(int num1,int num2,int ans)
{
ans = num1 * num2;
Console.WriteLine("Your Answer is: " + ans);
}
// Implement the divide function below here
public void Div(int num1,int num2,int ans)
{
ans = num1 / num2;
Console.WriteLine("Your Answer is: " + ans);
}
}
}
由于错误,没有实际输出
预期的输出是操作数输入将抛出if语句,该语句将抛出适当的操作并将ans推入控制台我将帮助您添加op,您将使用其他方法重复此操作: 首先, 第二,改变添加方法
public void Add(int num1, int num2)
{
ans = num1 + num2;
Console.WriteLine($"Your Answer is: {num1 + num2}");
}
首先,您没有向函数传递任何输入参数。你应该提供。 其次,在任何函数中都不需要名为
ans
的输入参数。
您可以按如下方式修改您的函数
public void Add(int num1,int num2){
int ans = num1+num2;
Console.WriteLine(ans);
}
也可以从方法返回值,例如:
public int Add(int num1,int num2){
int ans = num1+num2;
Console.WriteLine(ans);
return ans;
}
在输入法中,通过
Add(num1,num2)
您已经声明了像Add
这样的方法来获取参数,但是您调用它时没有指定任何参数。这就是问题所在。
public int Add(int num1,int num2){
int ans = num1+num2;
Console.WriteLine(ans);
return ans;
}