我尝试创建一个计算器程序,但是在除法中,我需要返回2个值,它们是默认参数的余数,而输出参数的商是我尽力研究的结果,但我无法正确获取答案
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;
}
}
}
答案 0 :(得分:1)
双重分裂提醒; //对于您的out参数
计算器c =新的Calculator();
double divResult = c.Division(33/8,divisionRemider);
divResult将为4
devisionRemider将为1
答案 1 :(得分:0)
您在这里错了
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;
您必须在主函数中声明新的变量以存储提醒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;
答案 2 :(得分:-1)
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;
}
}
}