如何打印前10的倍数,此程序打印10的下一个倍数 [英] How to print previous multiple of 10 , this program prints next multiple of 10

查看:129
本文介绍了如何打印前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;
}
}

解决方案

尝试使用函数来舍入数字:

  public   static   int  Round( int  x)
{
int leastDigit = x% 10 ;
return (x - leastDigit)+(leastDigit < 5 0 10 );
}


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;
		}
}

解决方案

Try using a function to round numbers:

public static int Round(int x)
   {
   int leastDigit = x % 10;
   return (x - leastDigit) + (leastDigit < 5 ? 0 : 10);
   }


这篇关于如何打印前10的倍数,此程序打印10的下一个倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆