P
P
P_Alexander2017-01-20 16:39:50
Java
P_Alexander, 2017-01-20 16:39:50

Why is the variable written incorrectly??

I need help, I can't understand where the number 2 comes from in my code, I checked everything everything works correctly, but these two come from nowhere!!! Help!
The answer should be 4 in advance, but the code returns 8!!
I understand that the code is not written beautifully, but the point is that in order to solve the problem Here is the code:

package testone;
import java.math.*;
import java.util.Scanner;


public class TestOne 
{
    
    BigDecimal s;
    BigDecimal f;
    BigDecimal h;
    BigDecimal y;
    public BigDecimal ceil (int a, int b)
    {
        s = BigDecimal.valueOf(a);
        h = BigDecimal.valueOf(b);
        y = BigDecimal.valueOf(1);
        
        //return ((a+b-1)/b);
        f = ((s.add(h).subtract(y)).divide(h,BigDecimal.ROUND_HALF_DOWN));
        System.out.println(" ceil ---" + f);
        return f;
    }
    
    public BigDecimal getRezult(int m, int n, int a)
    {
        h = ceil(m , a);
        System.out.println("hhhhhh ---" + h);
        y = ceil(n , a);
        System.out.println("yyyyyyyyyyyy ---" + h);
        System.out.println("h.multiply(y) -----" +  h.multiply(y));
        return h.multiply(y);
        //return ceil(m , a) * ceil(n , a);
    }
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
  int a = scan.nextInt();
        TestOne z = new TestOne();
        System.out.println(z.getRezult(m, n, a));
    }
}

The output of this code is:
6
6
4
ceil ---2
hhhhhh ---2
ceil ---2
yyyyyyyyyyy ---4 h.multiply
(y) -----8
8
BUILD SUCCESSFUL (total time: 6 seconds)
the question is, where in y, I got 4, if the function, as you can see from the output, returned 2?????!!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
artemsee, 2017-01-20
@artemsee

In the line
n = 6, a = 4;
Inside the method ceil(int a, int b), you assign
b -- it's the second parameter, i.e. a = 4; from the previous code snippet
Then you output
i.e. 4. Most likely in this line you wanted to display a variable y, and noth

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question