TheUnknown
TheUnknown

Reputation: 47

Using void parameters

There is something that I tried to fix over and over and I couldn't.

So the program asks a user for 3 numbers a then asks for the percentage they want to add then these numbers goes to a void method:

So its like this:

public static void main(String[] args) {

        System.out.print("Number 1: ");
        X1 = s.nextLong();
        System.out.print("Number 2: ");
        W1 = s.nextLong();
        System.out.print("Number 3: ");
        Z1 = s.nextLong();

        System.out.print("Percentage 1: ");
        int a = s.nextInt();
        System.out.print("Percentage 2: ");
        int b = s.nextInt();
        System.out.print("Percentage 3: ");
        int c = s.nextInt();
        Number(a,b,c);

}

public static void Number(int a, int b, int c)
{

    X1 = (long) (X1*(a/100 + 1));
    W1 = (long) (W1*(b/100 + 1));
    Z1 = (long) (Z1*(c/100 + 1));

}

But if I try to type the results the numbers don't change.

Note: X1,W1 and Z1 are all used as static long

And thanks in advance.

Upvotes: 3

Views: 126

Answers (3)

David Harkness
David Harkness

Reputation: 36532

You are dividing the integer parameters by the integer 100 using--you guessed it--integer arithmetic which results in 0 for any value less than 100. Use 100.0 to force floating point precision.

X1 = (long) (X1*(a / 100.0 + 1.0));
...

Upvotes: 0

Tyco
Tyco

Reputation: 131

Divide your number by 100.0 in the Number method.

Upvotes: 0

Ingo
Ingo

Reputation: 36329

Unless a,b,c >= 100, the expression

a/100 

will be 0. Hence

something * (a/100 +1) 

is

something * 1

Upvotes: 2

Related Questions