Java – write a program to round a number to the next multiple of 10 given the following conditions?

MyApproach

I created two functions in the first function, and I calculated the sum of all four numbers

In the second function, I check unitdigit, if > = 5 & & < = 9, and then continue with the set of statements given in the question Others I checked whether it was a number or two or any number If a number I return num = 0, otherwise I continue to use the statement set Sample input #1 sumrounded (11,15,23,30) sample output #1 80 (11 turns to 10,15 to 20,23 to 20 and 30 to 30) sample input #2 sumrounded (1,3,7,9) sample output #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;
             }
          }



     }

result:

Parameters           Actual Output    Expected Output 

'289' '3' '25' '308'   644              630

Solution

rounding

If the remainder is less than 5, subtract it from num Otherwise, add the ten minus remainder to num. it's like,

static int checkRound(int num) {
    int rem = num % 10;
    return rem < 5 ? num - rem : num + (10 - rem);
}

Or use math Round (float) or something

static int checkRound(int num) {
    return Math.round((float) num / 10) * 10;
}

Varargs

You can also implement sumbound as a varargs method using for each loop

static int sumRounded(int... nums) {
    int sum = 0;
    for (int num : nums) {
        sum += checkRound(num);
    }
    return sum;
}

test

Then you can test it,

public static void main(String[] args) {
    System.out.println(sumRounded(11,30)); // == 80
    System.out.println(sumRounded(1,9)); // == 20
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>