为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
慕妹3242003 2021-11-14 17:33:28
我试图创建一个计算器程序,但在除法方法中,我需要返回 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;}}}
查看完整描述

3 回答

?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

双除提醒;//对于你的输出参数

计算器 c = new Calculator();

double divResult = c.Division(33/8,divisionRemider);

divResult 将为 4

devisionRemider 将是 1


查看完整回答
反对 回复 2021-11-14
?
达令说

TA贡献1821条经验 获得超6个赞

你在这里做错了



    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;


查看完整回答
反对 回复 2021-11-14
?
RISEBY

TA贡献1856条经验 获得超5个赞

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;

}

}

}


查看完整回答
反对 回复 2021-11-14
  • 3 回答
  • 0 关注
  • 200 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信