[英]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.