只在for循环中打印每隔一个枚举值。

huangapple go评论79阅读模式
英文:

Only print every other enum value in for loop

问题

public enum Example{Red, Blue, Green, Yellow, Orange, Purple, Brown}

public class Test {
    public static void main(String[] args) {
        boolean print = false;
        for (Example colour: Example.values()) {
            if (print) {
                System.out.println(colour);
            }
            print = !print;
        }
    }
}
英文:

I'm trying to make it so only every other colour is being printed off in my for loop statement but because of the type difference I'm unable to incorporate it in an if statement.

public enum Example{Red, Blue, Green, Yellow, Orange, Purple, Brown}

public class Test {
    public static void main(String[] args) {
        for (Example colour: Example.values()) {
            if (????) {
                System.out.println(colour);
            }
        }
    }
}

Desired output:

Red Green Orange Brown

答案1

得分: 2

你可以使用ordinal()方法,就像这样:

for (Example colour : Example.values()) {
    if (colour.ordinal() % 2 == 0) {
        System.out.println(colour);
    }
}

或者,你可以使用外部变量来保存计数,类似于这样:

int i = 0;
for (Example colour : Example.values()) {
    if (i % 2 == 0) {
        System.out.println(colour);
    }
    i++;
}

但是,这似乎是一个适合使用传统的逐步增加为2for循环的地方,就像这样:

for (int i = 0; i < Example.values().length; i += 2) {
    System.out.println(Example.values()[i]);
}

这三种方法都会产生你所需的输出。

英文:

You could use the ordinal() like

for (Example colour : Example.values()) {
	if (colour.ordinal() % 2 == 0) {
		System.out.println(colour);
	}
}

Or, you could use an external variable to hold a count. Something like,

int i = 0;
for (Example colour : Example.values()) {
	if (i % 2 == 0) {
		System.out.println(colour);
	}
	i++;
}

But this seems like a good place for a traditional for loop that increments by 2. Like,

for (int i = 0; i &lt; Example.values().length; i += 2) {
	System.out.println(Example.values()[i]);
}

All three produce your requested output.

答案2

得分: 0

你可以使用 for 循环遍历每隔一个颜色:

for (int i = 0; i < Example.values().length; i += 2) {
    System.out.println(Example.values()[i]);
}
英文:

You could use a for loop to iterate over every other color:

for (int i =0; i&lt; Example.values().length;i+=2){
    System.out.println(Example.values()[i]);
}

答案3

得分: 0

你可以访问 Example.values() 并通过检查索引是否可被2整除(即 index % 2 == 0)来打印交替元素。

使用 Stream API 的示例:

import java.util.stream.IntStream;

enum Example {
    Red, Blue, Green, Yellow, Orange, Purple, Brown
}

public class Main {
    public static void main(String[] args) {
        IntStream.range(0, Example.values().length)
            .filter(i -> i % 2 == 0)
            .forEach(i -> System.out.println(Example.values()[i]));
    }
}

输出:

Red
Green
Orange
Brown

在线演示

你也可以使用传统的循环来代替使用 IntStream

英文:

You can access Example.values() and print the alternate elements by checking if the index is divisible by 2 (i.e. index % 2 == 0).

Demo using the Stream API:

import java.util.stream.IntStream;

enum Example {
	Red, Blue, Green, Yellow, Orange, Purple, Brown
}

public class Main {
	public static void main(String[] args) {
		IntStream.range(0, Example.values().length)
			.filter(i -&gt; i % 2 == 0)
			.forEach(i -&gt; System.out.println(Example.values()[i]));
	}
}

Output:

Red
Green
Orange
Brown

<kbd>ONLINE DEMO</kbd>

You can use the traditional loops instead of using an IntStream.

答案4

得分: 0

另一种使用循环的方法:

boolean toggle = false;
for (Example value : Example.values()) {
  if (toggle ^= true) System.out.println(value);
}
英文:

Another way using a loop:

boolean toggle = false;
for (Example value : Example.values()) {
  if (toggle ^= true) System.out.println(value);
}

huangapple
  • 本文由 发表于 2020年5月5日 07:12:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/61603170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定