打印数字的值会产生意外结果

时间:2014-11-18 21:46:00

标签: java

出于某种原因,我无法使下面的代码像它应该的那样工作。这就是我在java中所拥有的:

int num = 8; 

System.out.println("Value is now " + (num));
System.out.println("Value is now " + (num+2));
System.out.println("Value is now " + (num-4));
System.out.println("Value is now " + (num*5));
System.out.println("Value is now " + (num/3));
System.out.println("Value is now " + (++num));
System.out.println("Value is now " + (--num));

它打印以下内容:

Value is now 8
Value is now 10
Value is now 4
Value is now 40
Value is now 2
Value is now 9
Value is now 8

我期待得到这个:

Value is now 8
Value is now 10
Value is now 6
Value is now 30
Value is now 10
Value is now 11
Value is now 10

那么,我如何得到从一行到另一行的答案?我一直在阅读和搜索,找不到正确的方法。

7 个答案:

答案 0 :(得分:4)

一个常见的错误!您正在使用内联变量,这意味着变量本身不会被更改。为了实际更改变量,您需要使用'='。这将改变变量的实际值。

所以,例如,

int num = 8;

System.out.println("Value is now " + (num));
num = num + 2;
System.out.println("Value is now " + (num));
num = num - 4;
System.out.println("Value is now " + (num));
num = num * 5;
System.out.println("Value is now " + (num));
num = num / 3;
System.out.println("Value is now " + (num));
System.out.println("Value is now " + (++num));
System.out.println("Value is now " + (--num));

现在这是很多代码。 Java提供了一些很好的小功能,可以缩短它,称为“语法糖”。首先,'='将返回设置为变量的值。所以现在我们可以这样写:

int num = 8;

System.out.println("Value is now " + (num));
System.out.println("Value is now " + (num = num + 2));
System.out.println("Value is now " + (num = num - 4));
System.out.println("Value is now " + (num = num * 5));
System.out.println("Value is now " + (num = num / 3));
System.out.println("Value is now " + (++num));
System.out.println("Value is now " + (--num));

接下来,我们可以将这些操作写成速记。 num = num + 2变为num+=2num = num * 5变为num*=5,依此类推。

因此,我们可以将整个代码编写为:

int num = 8;

System.out.println("Value is now " + (num));
System.out.println("Value is now " + (num += 2));
System.out.println("Value is now " + (num -= 4));
System.out.println("Value is now " + (num *= 5));
System.out.println("Value is now " + (num /= 3));
System.out.println("Value is now " + (++num));
System.out.println("Value is now " + (--num));

度过美好的一天!

答案 1 :(得分:1)

您遇到的问题是您实际上并没有更改num的值。 Num总是8,所以你只是在数字8上打印各种操作。为什么不尝试这样的事情:

int num = 8; 
System.out.println("Value is now " + (num));
num += 2;
System.out.println("Value is now " + (num));
num -= 4;
System.out.println("Value is now " + (num));
num *= 5;
System.out.println("Value is now " + (num));
num /= 3;
System.out.println("Value is now " + (num));
num++;
System.out.println("Value is now " + (num));
num--;
System.out.println("Value is now " + (num));

编辑

进入命令行,收到预期的输出:

Value is now 8
Value is now 10
Value is now 6
Value is now 30
Value is now 10
Value is now 11
Value is now 10

答案 2 :(得分:1)

只需将新值分配给变量即可。

int num =8 
System.out.println("Value is now " + num);
num = num +2;
System.out.println("Value is now " + num);
num = num -4;
System.out.println("Value is now " + num);
num = num *5;
System.out.println("Value is now " + num);
......

答案 3 :(得分:0)

你现在这样做的方式并不是改变变量的值。

你可以阅读num-4作为"如果我从num&#34中减去4,那么该数字是多少。

你正在寻找类似num = num - 4的东西,这是"分配给num新值,等于从num&#34中减去4;

答案 4 :(得分:0)

您需要指定新值。

System.out.println("Value is now " + (num));
System.out.println("Value is now " + (num+=2));
System.out.println("Value is now " + (num-=4));
System.out.println("Value is now " + (num*=5));
System.out.println("Value is now " + (num/=3));
System.out.println("Value is now " + (++num));
System.out.println("Value is now " + (--num));

System.out.println可以打印出控制台所需的所有变量。

答案 5 :(得分:0)

除了最后两行之外,在应用数学运算时,您只需给出println变量的值。最后两行有效,因为++ x / - x代表x = x + 1或减号。您需要以这种方式分配变量,以使代码按指定的方式工作。

答案 6 :(得分:0)

在Java中,原语(例如:int)是“不可变的”。这意味着你不能直接改变它们(AKA改变它们),你只能创建新的。

如果您想有效修改int num,则必须重新分配。{p>这将使用新值覆盖当前值。这是一个例子:

int num = 8;
System.out.println("Value is now " + num);
num = num + 2;
System.out.println("Value is now " + num);
num = num + 2;

但也许更好的可视化方法就是这样想:

int num1 = 8;
System.out.println("Value is now " + num1);
int num2 = num1 + 2;
System.out.println("Value is now " + num2);
int num3 = num2 + 2;

我说这是一种更好的可视化方法,因为在原始示例中,您不是更改 num,而只是重新分配它。第二个例子是使其更加明确。