本文介绍了如何打印前10的倍数,此程序打印10的下一个倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
定义一个返回三个舍入数字之和的方法。如果数字的最右边数字小于5,则将其值舍入为前一个10的倍数,否则如果数字的最右边数字大于或等于5,则舍入到下一个10的倍数。 / pre>
我尝试过:
public class Roundofff {
public static void main(String [] args){
int a = 23,b = 34,c = 69;
System.out.println(sumOfMultiples(a,b,c));
}
public static int sumOfMultiples(int a,int b,int c){
if(a< =&& b< = 0& & c< = 0){
返回-1;
}
a = a +(10 - a%10);
b = b +(10 - b%10);
c = c +(10 - c%10);
返回a + b + c;
}
}解决方案
Define a method which returns the sum of three rounded numbers. If the right most digit of the number is lessthan 5, then round off it's value to the previous multiple of 10 otherwise if the right most digit of the number is greater or equal to 5, then round off to the next multiple of 10.
What I have tried:public class Roundofff { public static void main(String[] args) { int a = 23, b = 34, c = 69; System.out.println(sumOfMultiples(a, b, c)); } public static int sumOfMultiples(int a, int b, int c) { if(a <= 0 && b <= 0 && c <= 0){ return -1; } a = a + (10 - a % 10); b= b + (10 - b % 10); c= c + (10 - c % 10); return a + b +c; } }解决方案
这篇关于如何打印前10的倍数,此程序打印10的下一个倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!