gpt4 book ai didi

Generate even numbers in an int array?(是否在整型数组中生成偶数?)

转载 作者:bug小助手 更新时间:2023-10-25 11:41:22 26 4
gpt4 key购买 nike



I need to make a 10 element array and the goal is to print out the even numbers from 2 to 20. I have to do this by adding 2 to the beginning element. This is what I have so far. I think I should use a loop as shown but I don't know how to go about adding 2 and printing that out.

我需要创建一个10元素数组,目标是打印出从2到20的偶数。为此,我必须在开始元素上加上2。这就是我到目前为止所拥有的。我想我应该使用如图所示的循环,但我不知道如何添加2并将其打印出来。


int[] array = new int[10];

for(int counter=0; counter<array.length; counter++) {

}

更多回答
优秀答案推荐

if you want program print even number between 2 & 20

如果您想要程序打印2到20之间偶数



for(int i=2;i<=20;i++)
{
if(i%2 == 0)
print(i)
}


Start at 2 and increment by 2 to get the even numbers:

从2开始,再递增2,得到偶数:



int[] array = new int[10]

for(int counter=2; counter <= 20; counter += 2) {
array[counter/2 - 1] = counter
}


or



int[] array = new int[10]

for(int i=0; i <= 10; i++) {
array[i] = i*2 + 2
}


int[] array = new int[10];

for (int i = 0, j = 1; i < array.length && j <= 20; j++) {
if (j % 2 == 0) {
array[i] = j;
i++;
}
}

System.out.println(Arrays.toString(array));


Java 8: int[] array = IntStream.range(1, 11).map(x -> x * 2).toArray();

Java 8:int[]数组=IntStream.range(1,11).map(x->x*2).toArray();



Or, to just print: IntStream.range(1, 11).map(x -> x * 2).forEach(System.out::println);

或者,只打印:IntStream.range(1,11).map(x->x*2).forEach(System.out::println);



From java-9 you can use IntStream.iterate to create int array

在Java-9中,您可以使用IntStream.iterate创建int数组



int[] arr= IntStream.iterate(2, i->i<=20, i->i+2).toArray();


for Integer array you can use Stream.iterate

对于整型数组,可以使用Stream.iterate



Integer[] ary = Stream.iterate(2, i->i<=20, i->i+2).toArray(Integer[]::new);


Using your code as a start, this will work:

使用您的代码作为起点,这将会起作用:



int[] array = new int[10];

for(int counter=0; counter<array.length; counter++) {
array[counter] = (counter + 1) * 2;
}
System.out.println(Arrays.toString(array));


The following will also work with Eclipse Collections:

下面的代码还将与Eclipse集合一起使用:



int[] array = IntInterval.evensFromTo(2, 20).toArray();
System.out.println(Arrays.toString(array));


Note: I am a committer for Eclipse Collections

注意:我是一名Eclipse集合的提交者



this is also an option

这也是一种选择


int i, value;
int nums[] = new int[10];
for (i = 0, value = 2; i < nums.length; value = value + 2, i = i + 1) {
nums[i] = value;
}
for (i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}


Below is an example using do-while.

下面是一个使用do-While的示例。


you can simply define a range of expected even numbers using minVal and maxVal.

你可以简单地使用minVal和maxVal定义一个期望的偶数范围。


Program will execute and return list of ordered even numbers for given range. Assuming input values are correct even numbers. You can improve to apply validations.

程序将执行并返回给定范围内的有序偶数列表。假设输入值是正确的偶数。您可以改进以应用验证。


public class EvenNumberGenerator {

static int minVal=2; //enter valid min value even number
static int maxVal = 20; //enter valid max value even number

public static void main(String[] args) {
List<Integer> evenNumbers = new ArrayList();
do {
if(minVal % 2 == 0) {
evenNumbers.add(minVal);
}
minVal++;
} while (!evenNumbers.contains(maxVal));

System.out.println(evenNumbers);

// evenNumbers.toArray(); in case you need an array
}
}

OUTPUT

输出量


[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]


for (int i = 0, j = 0; i < array.length; i++, j += 2)
array[i] = j;

更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com