Java 一个接一个地分配真值或假值

Java 一个接一个地分配真值或假值,java,Java,我的作业中有一个问题,我找不到解决办法 此数组声明12个变量 boolean[] rowOfRotatoes = new boolean[12]; 现在我必须一个接一个地分配真值和假值 rowOfRotatoes[0] = true; rowOfRotatoes[1] = false; rowOfRotatoes[2] = true; rowOfRotatoes[3] = false; rowOfRotatoes[4] = true; .... rowOfRotatoes[9] = true;

我的作业中有一个问题,我找不到解决办法

此数组声明12个变量

boolean[] rowOfRotatoes = new boolean[12];
现在我必须一个接一个地分配真值和假值

rowOfRotatoes[0] = true;
rowOfRotatoes[1] = false;
rowOfRotatoes[2] = true;
rowOfRotatoes[3] = false;
rowOfRotatoes[4] = true;
....
rowOfRotatoes[9] = true;
rowOfRotatoes[10] = false;
rowOfRotatoes[11] = true;
但是我必须使用一个循环来完成这个任务

他们给了我一个结构来填补空白

int plantingSpace = 0;
while(plantingSpace < 12) {

   rowOfRotatoes[plantingSpace] = <Fill this space 1> <Fill this space 2> <Fill this space 3> == 0;

   ++plantingSpace;
}

如何使用上述结构依次分配真值和假值?

通过检查索引是否为偶数,可以使用模运算符%来完成此操作。这将导致赋值的右侧在true和false之间交替


通过检查索引是否为偶数,可以使用模运算符%。这将导致赋值的右侧在true和false之间交替


检查每个循环的plantingSpace是偶数还是奇数,并相应地设置布尔值真/假。

检查每个循环的plantingSpace是偶数还是奇数,并相应地设置布尔值真/假。

严格按照您的要求填充空格:

int plantingSpace = 0;
while (plantingSpace < 12) {  
   rowOfRotatoes[plantingSpace] = plantingSpace % 2 == 0;   
   ++plantingSpace;
}

要严格按照您的要求填充空间,请执行以下操作:

int plantingSpace = 0;
while (plantingSpace < 12) {  
   rowOfRotatoes[plantingSpace] = plantingSpace % 2 == 0;   
   ++plantingSpace;
}

容易的孩子。。。你喜欢这样吗

        Boolean rowOfRotatoes[]=new Boolean[13];
        int plantingSpace = 0;
        while(plantingSpace < 12) 
        {
           boolean value= (plantingSpace %2==0)?true:false; 
           rowOfRotatoes[plantingSpace] = value;

           ++plantingSpace;
        }
这里我使用了三元运算符,它检查条件,然后根据条件返回false或true,然后进入值

plantingSpace%2==0?真:假


容易的孩子。。。你喜欢这样吗

        Boolean rowOfRotatoes[]=new Boolean[13];
        int plantingSpace = 0;
        while(plantingSpace < 12) 
        {
           boolean value= (plantingSpace %2==0)?true:false; 
           rowOfRotatoes[plantingSpace] = value;

           ++plantingSpace;
        }
这里我使用了三元运算符,它检查条件,然后根据条件返回false或true,然后进入值

plantingSpace%2==0?真:假

您可以简单地使用切换布尔变量:

boolean[] rowOfRotatoes = new boolean[12];
int plantingSpace = 0;
boolean toggler = true;

while (plantingSpace < rowOfRotatoes.length) {
    rowOfRotatoes[plantingSpace++] = toggler;
    toggler = !toggler;
}
表示您可以通过更改变量的初始值来更改真/假条目的顺序。

您只需使用切换布尔变量即可:

boolean[] rowOfRotatoes = new boolean[12];
int plantingSpace = 0;
boolean toggler = true;

while (plantingSpace < rowOfRotatoes.length) {
    rowOfRotatoes[plantingSpace++] = toggler;
    toggler = !toggler;
}

表示可以通过更改变量的初始值来更改true/false条目的顺序。

这里有一个解决方案,从数组的第一个元素具有true as值开始:

int plantingSpace = 0;
while(plantingSpace < 12) {
   rowOfRotatoes[plantingSpace] = (plantingSpace % 2 == 0);
   ++plantingSpace;
}
说明:

表达式plantingSpace%2==0检查plantingSpace除以2=模的余数是否等于零

如果表示plantingSpace为偶数,则数组中的相应值将设置为true。
如果这并不意味着plantingSpace不均匀,则相应的值将设置为false。

这里有一个解决方案,从数组的第一个元素具有true作为值开始:

int plantingSpace = 0;
while(plantingSpace < 12) {
   rowOfRotatoes[plantingSpace] = (plantingSpace % 2 == 0);
   ++plantingSpace;
}
说明:

表达式plantingSpace%2==0检查plantingSpace除以2=模的余数是否等于零

如果表示plantingSpace为偶数,则数组中的相应值将设置为true。
如果这并不意味着plantingSpace是不均匀的,则相应的值将设置为false。

为什么要标记此问题?我在里面看不到任何与Javascript有关的东西……为什么要将这个问题标记为Javascript?我认为它与Javascript无关……我想你应该考虑一下,如果为true,那么为false,那么语句可以简化为什么。如果为true,那么我做了什么PIn您的三元语句。当我们有三元运算符时,为什么要使用if!!我再试一次。如果为真,否则为假,这是同一回事吗?真:假,它们都可以简化为。我想你应该考虑一下,如果是真的话,那么其他的假可以简化为什么。如果是,我做了什么PIn您的三元语句。当我们有三元运算符时,为什么要使用if!!我再试一次。如果为真,否则为假,这是同一回事吗?对:错,它们都可以简化为正义。这很酷,非常有趣efficient@anshulkatta谢谢:是的,是的。@Michael,接受这个答案,这是最好的:这很酷,非常好efficient@anshulkatta谢谢:是的,是的。@Michael,接受这个答案,这是最好的: