Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中调用数组上的if语句_Java_Arrays - Fatal编程技术网

如何在java中调用数组上的if语句

如何在java中调用数组上的if语句,java,arrays,Java,Arrays,如何检查二进制数组的第一个数字是否为1 当前,如果我运行,我会得到类似{1,0,1}ect的输出您只需执行以下操作: import java.util.Random; public class Console { public static void main(String[] args) { while (3>2) { Random rand1 = new Random(); Random rand2 = new Random(); Random rand3 = new Random(

如何检查二进制数组的第一个数字是否为1

当前,如果我运行,我会得到类似{1,0,1}ect的输出

您只需执行以下操作:

import java.util.Random;

public class Console {
public static void main(String[] args) {
while (3>2) {
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
Random rand5 = new Random();
Random rand6 = new Random();
Random rand7 = new Random();
Random rand8 = new Random();
int onenum = rand1.nextInt(2);
int twonum = rand2.nextInt(2);
int threenum = rand3.nextInt(2);
int fournum = rand4.nextInt(2);
int fivenum = rand5.nextInt(2);
int sixnum = rand6.nextInt(2);
int sevennum = rand7.nextInt(2);
int eightnum = rand8.nextInt(2);
int binary[] = {onenum, twonum, threenum, fournum, fivenum, sixnum, sevennum, eightnum};
    System.out.println(java.util.Arrays.toString(binary));
0是数组中的第一个元素,因为数组的事实是基于0的

旁注:您不必从Random中声明新对象来获取数字,您可以使用相同的对象,如:

if(binary[0] == 1) {
    //do something
}

您可以使用索引访问数组特定位置的元素:

Random rand = new Random();
int onenum = rand.nextInt(2);
int twonum = rand.nextInt(2);
int threenum = rand.nextInt(2);
// and so on
在您的情况下,要检查第一个元素,它将是:

if (someArray[position] == something)
注:

大多数编程语言中的索引都以0开头,因此第一个元素将位于数组的索引0中。第二个在索引1中。等等
天哪,我可以大大减少你的代码!!分析并查看此代码是否与您的代码相同

if (binary[0] == 1)

只需执行此操作即可检查第一个元素是否等于1:

while (3>2) {
    Random rand = new Random();
    int[] binary = new int[8];
    for(int i=0;i<8;i++)
        binary[i] = rand.nextInt(2);
    if(binary[0] == 1)
         //if first number is 1
}
关于代码的其他问题

我意识到你使用的是3>2。如果希望它是一个无限循环,可以使用whiletrue

只需创建一个随机对象即可创建多个随机数。像这样做

您甚至可以使用数组来存储随机数:

Random rnd = new Random();
oneNum = rnd.nextInt(2);
twoNum = rnd.nextInt(2);
threeNum = rnd.nextInt(2);

为什么当3>2时会有一个条件,你想要一个无限循环吗?这段代码看起来很可爱。尤其是3>2…:-D
Random rnd = new Random();
oneNum = rnd.nextInt(2);
twoNum = rnd.nextInt(2);
threeNum = rnd.nextInt(2);
Random rnd = new Random();
int[] num = new int[8];
for (int x=0; x<num.length; x++)
    num[x] = rnd.nextInt(2);