C#中的默认参数和输出参数相同的方法

我试图创建一个计算器程序,但在除法方法中,我需要返回 2 个值,它们是默认参数中的余数和 out 参数中的商我尽了最大努力,但我无法正确得到答案


using System;

public class Calculator

{

public int Addition(int a, int b){

int sum = a+b;

return sum;

}

public int Subtraction(int a, int b){

int diff = a-b;

return diff;

}

public int Multiplication(int a, int b){

int mul = a*b;

return mul;

}

public double Division(int a, int b, out double remainder){

double div = a/b;

double rem = a%b;

remainder = rem;

return div;

}

}

public class program{

public static void Main(){

Calculator c  = new Calculator();

Console.WriteLine("Enter the operator");

char s=Convert.ToChar(Console.ReadLine());

Console.WriteLine("Enter the operands");

int a=Convert.ToInt32(Console.ReadLine());

int b=Convert.ToInt32(Console.ReadLine());

switch(s)

{

case '+':

int summ = c.Addition(a,b);

Console.WriteLine("Result of {0} + {1} is {2}",a,b,summ);

break;

case '-':

int sub = c.Subtraction(a,b);

Console.WriteLine("Result of {0} - {1} is {2}",a,b,sub);

break;

case'*':

int mul = c.Multiplication(a,b);    

Console.WriteLine("Result of {0} * {1} is {2}",a,b,mul);

break;

case '/':

double div = c.Division(a,b,out a);

Console.WriteLine("Result of {0} / {1} is {2} and rem is {3}",a,b,div,a);

break;

default:

Console.WriteLine("Invalid Operand");

break;

}

}

}


慕妹3242003
浏览 201回答 3
3回答

幕布斯6054654

双除提醒;//对于你的输出参数计算器 c = new Calculator();double divResult = c.Division(33/8,divisionRemider);divResult 将为 4devisionRemider 将是 1

达令说

你在这里做错了    case '/':    double div = c.Division(a,b,out a);    Console.WriteLine("Result of {0} / {1} is {2} and rem is {3}",a,b,div,a);    break;你必须在 main 函数中声明新的变量来存储提醒 EX: int rem;解决办法是:case '/':int rem;double div = c.Division(a,b,out rem);Console.WriteLine("Result of {0} / {1} is {2} and rem is {3}",a,b,div,rem);break;

RISEBY

using System;public class Calculator{public int Addition(int a, int b){int sum = a+b;return sum;}public int Subtraction(int a, int b){int diff = a-b;return diff;}public int Multiplication(int a, int b){int mul = a*b;return mul;}public double Division(int a, int b, out double remainder){double div = a/b;double rem = a%b;remainder = rem;return div;}}public class program{public static void Main(){double remainder;Calculator c  = new Calculator();Console.WriteLine("Enter the operator");char s=Convert.ToChar(Console.ReadLine());Console.WriteLine("Enter the operands");int a=Convert.ToInt32(Console.ReadLine());int b=Convert.ToInt32(Console.ReadLine());    switch(s){case '+':int summ = c.Addition(a,b);Console.WriteLine("Result of {0} + {1} is {2}",a,b,summ);break;case '-':int sub = c.Subtraction(a,b);Console.WriteLine("Result of {0} - {1} is {2}",a,b,sub);break;case'*':int mul = c.Multiplication(a,b);    Console.WriteLine("Result of {0} * {1} is {2}",a,b,mul);break;case '/':double div = c.Division(a,b,out remainder);Console.WriteLine("Result of {0} / {1} is {2} and rem is {3}",a,b,div,remainder);break;default:Console.WriteLine("Invalid Operand");break;}}}
打开App,查看更多内容
随时随地看视频慕课网APP