Write a program to Round a number to the next multiple of 10 if its ones digit is 5 or more, otherwise round it the previous multiple of 10.So, 25 and 26 round to 30 where as 23 and 24 round to 20. 20 also rounds to 20. You have been given 4 ints as input. Round each of the input values and return their sum.
MyApproach
我在第一个函数中创建了2个函数,我计算了所有4个数字的总和.
在第二个函数中,我检查了UnitDigtit,如果> = 5&&< = 9然后继续问题中给出的一组语句. 其他 我检查了它是一个数字还是两个数字或任何数字.如果一个数字我返回num = 0否则我继续使用语句集. 样本输入#1 sumRounded(11,15,23,30) 样本输出#1 80(11轮到10,15到20,23到20和30到30) 样本输入#2 sumRounded(1,3,7,9) 样本输出#2 20
public int sumRounded(int num1, int num2, int num3, int num4)
{
int a=checkRound(num1);
int b=checkRound(num2);
int c=checkRound(num3);
int d=checkRound(num4);
return a+b+c+d;
}
public int checkRound(int num)
{
int a=num%10;
if((a>=5) &&(a<=9))
{
if(a==5)
{
num=num+5;
}
else if(a==6)
{
num=num+6;
}
else if(a==7)
{
num=num+7;
}
else if(a==8)
{
num=num+8;
}
else if(a==9)
{
num=num+9;
}
return num;
}
else
{
if((num/10)!=0)
{
if(a==1)
{
num=num-1;
}
else if(a==2)
{
num=num-2;
}
else if(a==3)
{
num=num-3;
}
else if(a==4)
{
num=num-4;
}
return num;
}
else
{
return num=0;
}
}
}
结果:
Parameters Actual Output Expected Output
'289' '3' '25' '308' 644 630