加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 04:30:39 所属栏目:Java 来源:网络整理
导读: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

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

解决方法

四舍五入

如果余数小于5,则从num减去它.否则,将十个减去余数添加到num.就像是,

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

或者使用Math.round(float)之类的

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

Varargs

您还可以使用for-each loop将sumRounded实现为varargs方法

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

测试

那你就可以测试一下,

public static void main(String[] args) {
    System.out.println(sumRounded(11,30)); // == 80
    System.out.println(sumRounded(1,9)); // == 20
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读