Java 如何将多个 int 设置为相同的数字?爪哇
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19442283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set multiple int's equal to the same number ? Java
提问by user2893243
I have the code int index, r, g, b;
and I want to set all of them equal to a certain number, I don't individually want to write index = 0; r = 0;
... and so on.
我有代码int index, r, g, b;
,我想将它们全部设置为某个数字,我不想单独写index = 0; r = 0;
......等等。
How do I do this in java? index,r,g,b = 0;
doesn't work for me.
我如何在java中做到这一点?index,r,g,b = 0;
对我不起作用。
采纳答案by Aditya Vikas Devarapalli
use this line of code for initializing all the variables at once
使用这行代码一次初始化所有变量
r = g = b = index = 0;
or else initialize when you declare like:
或者在您声明时初始化:
int index=0, r=0, g=0, b=0;
回答by hexacyanide
Initialize all the values, then set the values.
初始化所有值,然后设置值。
int index, r, g, b;
index = r = g = b = 0;