如何在Java中打印数组的偶数索引?
我应该编写一个程序,使用for循环打印出数组的偶数索引。例如,如果我创建一个有10个数字的数组,它的索引从0到9,因此在这种情况下,我将打印出索引2、4、6和8处的数字。这是我到目前为止写的,但它不起作用。请注意,我并没有试图打印出数组的偶数。我想要的只是偶数索引 示例I输入以下数组:如何在Java中打印数组的偶数索引?,java,arrays,Java,Arrays,我应该编写一个程序,使用for循环打印出数组的偶数索引。例如,如果我创建一个有10个数字的数组,它的索引从0到9,因此在这种情况下,我将打印出索引2、4、6和8处的数字。这是我到目前为止写的,但它不起作用。请注意,我并没有试图打印出数组的偶数。我想要的只是偶数索引 示例I输入以下数组:3,7,5,5,5,7,7,9,9,3 程序输出: 5 // (the number at index 2) 5 // (the number at index 4) 7 // (the number at ind
3,7,5,5,5,7,7,9,9,3
程序输出:
5 // (the number at index 2)
5 // (the number at index 4)
7 // (the number at index 6)
9 // (the number at index 8)
我的代码:
public class Arrayevenindex
{
public static void main(String[] args)
{
int number; // variable that will represent how many elements the user wants the array to have
Scanner key = new Scanner(System.in);
System.out.println(" How many elements would you like your array to have");
number = key.nextInt();
int [] array = new int [number];
// let the user enter the values of the array.
for (int index = 0; index < number; index ++)
{
System.out.print(" Value" + (index+1) + " :");
array[index] = key.nextInt();
}
// Print out the even indexes
System.out.println("/nI am now going to print out the even indexes");
for (int index = 0; index < array.length; index ++)
{
if (array[number+1]%2==0)
System.out.print(array[number]);
}
}
}
公共类ArrayEventIndex
{
公共静态void main(字符串[]args)
{
int number;//表示用户希望数组包含多少元素的变量
扫描仪键=新扫描仪(System.in);
println(“您希望您的数组包含多少元素”);
number=key.nextInt();
int[]数组=新的int[number];
//让用户输入数组的值。
对于(int index=0;index
您只需更改for循环,并去掉内部IF
for( int index = 0; index < array.length; index += 2) {
System.out.println(array[index]);
}
for(int index=0;index
您只需更改for循环,并去掉内部IF
for( int index = 0; index < array.length; index += 2) {
System.out.println(array[index]);
}
for(int index=0;index
使用Java8Stream
API完全一样
Integer[] ints = {0,1,2,3,4,5,6,7,8,9};
IntStream.range(0, ints.length).filter(i -> i % 2 == 0).forEach(i -> System.out.println(ints[i]));
使用Java8
Stream
API也完全一样
Integer[] ints = {0,1,2,3,4,5,6,7,8,9};
IntStream.range(0, ints.length).filter(i -> i % 2 == 0).forEach(i -> System.out.println(ints[i]));
我想这就足够了
// For loop to search array
for (int i = 0; i < array.length; i++) {
// If to validate that the index is divisible by 2
if (i % 2 == 0) {
System.out.print(array[i]);
}
}
//用于搜索数组的循环
for(int i=0;i
我想这就足够了
// For loop to search array
for (int i = 0; i < array.length; i++) {
// If to validate that the index is divisible by 2
if (i % 2 == 0) {
System.out.print(array[i]);
}
}
//用于搜索数组的循环
for(int i=0;i
这就是我所做的,它是有效的:而且我没有打印出索引[0],因为从技术上讲,这甚至不是我在2开始for循环的原因。你的帖子确实帮了我很大的忙。我也感谢所有花时间发布答案的人
import java.util.Scanner;
public class Arrayevenindex
{
public static void main(String[] args)
{
int number; // variable that will represent how many elements the user wants the array to have
Scanner key = new Scanner(System.in);
System.out.println(" How many elements would you like your array to have");
number = key.nextInt();
int [] array = new int [number];
// let the user enter the values of the array.
for ( int index = 0; index < number; index ++)
{
System.out.print(" Value" + (index+1) + " :");
array[index] = key.nextInt();
}
// Print out the even indexes
System.out.println("/nI am now going to print out the even indexes");
for ( int index = 2; index < array.length; index +=2)
{
System.out.print(array[index] + " ");
}
}
import java.util.Scanner;
公共类ArrayEventIndex
{
公共静态void main(字符串[]args)
{
int number;//表示用户希望数组包含多少元素的变量
扫描仪键=新扫描仪(System.in);
println(“您希望您的数组包含多少元素”);
number=key.nextInt();
int[]数组=新的int[number];
//让用户输入数组的值。
对于(int index=0;index
}这就是我所做的,而且它是有效的:而且我没有打印出索引[0],因为从技术上讲,这不是我在2开始for循环的原因。你的帖子确实帮了我很大的忙。我也感谢所有花时间发布答案的人
import java.util.Scanner;
public class Arrayevenindex
{
public static void main(String[] args)
{
int number; // variable that will represent how many elements the user wants the array to have
Scanner key = new Scanner(System.in);
System.out.println(" How many elements would you like your array to have");
number = key.nextInt();
int [] array = new int [number];
// let the user enter the values of the array.
for ( int index = 0; index < number; index ++)
{
System.out.print(" Value" + (index+1) + " :");
array[index] = key.nextInt();
}
// Print out the even indexes
System.out.println("/nI am now going to print out the even indexes");
for ( int index = 2; index < array.length; index +=2)
{
System.out.print(array[index] + " ");
}
}
import java.util.Scanner;
公共类ArrayEventIndex
{
公共静态void main(字符串[]args)
{
int number;//表示用户希望数组包含多少元素的变量
扫描仪键=新扫描仪(System.in);
println(“您希望您的数组包含多少元素”);
number=key.nextInt();
int[]数组=新的int[number];
//让用户输入数组的值。
对于(int index=0;index
}
如果(数组[number+1]%2==0)
->您不想改为检查索引吗?您能更明确地说明它是如何“不工作”的吗?您是否尝试过在调试器中单步执行并在那里查找问题?很明显,您测试的值是错误的。if(array[number+1]%2==0)
->您不想改为检查index
吗?您能更明确地说明它是如何“不工作”的吗?您是否尝试过在调试器中单步执行并在那里查找问题?很明显,您正在测试错误的值。