有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我在第一个函数中创建了两个函数,我计算了所有4个数字的总和

在第二个函数中,我检查了UnitDigtit if>=5&&<=9然后进行问题中给出的一系列陈述

否则

我检查了它是一位数字还是两位数字或任何数字。如果有一个数字我返回num=0,否则我继续处理语句集

样本输入#1

四舍五入(11,15,23,30)

样本输出#1

80(11轮对10轮、15轮对20轮、23轮对20轮和30轮对30轮)

样本输入#2

四舍五入(1,3,7,9)

样本输出#2

二十

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) 个答案

  1. # 1 楼答案

    只需在chexkRound方法中输入以下代码

     int a = num % 10;
     if ((a >= 5) && (a <= 9)) {
         num = 10 * (num / 10 + 1);
     } else {
         num = 10 * (num / 10);
     }
     return num;
    
  2. # 2 楼答案

    如果你想修正你的方法,请执行以下操作,但埃克斯伯里的答案是一个较短的方法

    1. 如果你要四舍五入,你应该把10加上数字,而不是数字本身。例如,26向上取整应该是26+(10-6)=26+(4)=30
  3. # 3 楼答案

    四舍五入

    如果余数小于5,则从num中减去它。否则,将10减去余数加到num。比如

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

    ^{}一样使用

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

    Varargs

    还可以使用^{} loopsumRounded实现为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, 15, 23, 30)); // == 80
        System.out.println(sumRounded(1, 3, 7, 9)); // == 20
    }
    
  4. # 4 楼答案

    下面的代码非常适合您的需要

    //代码开始

        public static void main(String[] args) {
        System.out.println("The answer is: " + getSum(2,5,19));
    }
    
    public static int getSum(int... nums) {
        int sum = 0;
        for (int n : nums) {
            int lastDigit = n % 10;
            if (lastDigit >= 5) {
                sum = sum + n + (10 - (lastDigit));
            } else {
                sum = sum + 10 * (n / 10);
            }
        }
        return sum;
    }
    

    //代码结束

  5. # 5 楼答案

    只需使用提醒检查inside checkRounded方法即可

    int number = 23;
    int output=0;
    if(number%10<5){
        output=(number/10)*10;
    }
    else{
        output=((number/10)+1)*10;
    }
    
    System.out.println(output);