java输入10的倍数_java – 在给定以下条件的情况下编写一个程序将数字舍入到下一个10的倍数?...

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值