简体   繁体   English

简单的java为什么变量不会改变?

[英]Simple java why variable doesn't change?

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? 从这段代码我得到5但不应该得到2? how should I change the code to make it equal to 2 instead of "sum=lowest;"? 我应该如何更改代码使其等于2而不是“sum =最低”?

Because assignment is the other way around. 因为分配是另一种方式。 It is like: 它像是:

variable = new value;

So, you want: 所以你要:

lowest = sum;

I'm not sure what you're trying to do, but you never alter the value of lowest , but you assign lowest to sum 我不确定你要做什么,但你永远不会改变lowest值,但是你将lowest值分配给sum

Do you mena to print the value of sum ? 你知道打印sum的价值吗?

Change 更改

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

to

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

if you are trying to get lowest to be equal to 2 . 如果你想把最低等于2

If you want 2 , just do: 如果你想要2 ,那就做:

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

lowest = sum. 最低=总和。

by doing "sum = lowest" you are assigning the value of lowest to sum. 通过执行“sum =最低”,您将最低值分配给sum。 Assignments work right to left 作业从右到左工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么增量运算符不更改Java中变量的值? - Why doesn't the increment operator change the value of variable in java? 为什么这个简单的java fork join pool不起作用? - Why doesn't this simple java fork join pool work? 尝试更改变量值时,Java线程不起作用 - Java Thread doesn't work when trying to change variable value 为什么Java中的volatile不更新变量的值? - Why doesn't volatile in Java update the value of a variable? 为什么java不允许在方法内初始化静态变量? - Why java doesn't allow initializing static variable inside a method? 为什么final变量不需要在java的main方法中初始化? - Why Final variable doesn't require initialization in main method in java? Java 8:为什么Final变量实际上不适用于switch块? - Java 8: Why Effectively Final variable doesn't work for switch block? 为什么布尔表达式的值不改变? (JAVA) - Why doesn't the value of the boolean expression change? (Java) 为什么当我尝试在这种情况下更改变量时,它不会在该循环外更改变量 - Why is it that when i try to change a variable in this case statement it doesn't change the variable outside that loop 关于为什么这个变量没有按预期改变的 Java 多线程问题 - Java multithreading problem about why this variable didn't change as hoped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM