0
public class Practice {
    public static void main( String args[] )
    {
        int lowest= 5;
        int sum = 2;
        if (lowest>sum){
            sum=lowest; 
        }
        System.out.println( lowest );
    }   
}

From this code I, get 5 but shouldn't I get 2? how should I change the code to make it equal to 2 instead of "sum=lowest;"?

2
  • 7
    Assignment is right to left. Commented Apr 4, 2014 at 22:42
  • "a = b" will assign value of "b" to "a", not value of "a" to "b"... Commented Apr 4, 2014 at 22:42

5 Answers 5

3

Because assignment is the other way around. It is like:

variable = new value;

So, you want:

lowest = sum;
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure what you're trying to do, but you never alter the value of lowest, but you assign lowest to sum

Do you mena to print the value of sum ?

Comments

0

If you want 2, just do:

    if (lowest>sum){
        lowest=sum; 
    }

Comments

0

Change

if (lowest > sum){
    sum = lowest; 
}

to

if (lowest > sum){
    lowest = sum; 
}

if you are trying to get lowest to be equal to 2.

Comments

0

lowest = sum.

by doing "sum = lowest" you are assigning the value of lowest to sum. Assignments work right to left

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.