这里需要一些语法帮助。我已经定义了类型之后尝试重新初始化多个变量。例如,
int bonus, sales, x, y = 50;
这很好用......但是我想稍后在我的程序中为这些变量添加一个不同的值,但是我收到了语法错误。
bonus = 25, x = 38, sales = 38;
编译器说我需要
another semicolon for bonus and x
。
有没有办法在一行代码中更改值?或者我必须在每个值之后加一个分号?
答案 0 :(得分:2)
我认为你对int bonus, sales, x, y = 50;
的行为感到困惑。它将y
初始化为50
,其余未初始化。
要将所有这些内容初始化为50
,您必须:
int bonus = 50, sales = 50, x = 50, y = 50;
然后您可以更改其值:
bonus = 25;
x = 38;
sales = 38;
// or compact
bonus = 25; x = 38; sales = 38;
// or to same value
bonus = x = sales = 42;
与您可以在任何地方使用逗号语法的C
语言不同,在Java中,您只能在声明变量时使用它,或者在for
循环中使用:for (i=1, j=2; i < 10; i++, j+=2)
答案 1 :(得分:0)
int bonus = 25;
int x = 38; // or any other value you prefer
int sales = 38;
以后你可以访问变量
bonus = 35; // and so on...
答案 2 :(得分:-1)
方法:你可以这样使用它。
int bonus=50; sales=50; x=50; y = 50;
在您的代码中,只有y已初始化。
如果有任何帮助访问: http://wwww.logic4code.blogspot.in/