我在 Java 中遇到问题:
Given a string, return a string made of the chars at indexes 0,1,4,5,8,9...
我知道如何解决它,但是我想知道我是否可以在 for 循环增量本身中使用 if-else
,例如:
for (int i=0; i < str.length(); if (i%4==0) i++, else i+=3){
result += str.charAt(i);
}
我们可以做类似的事情吗?
最佳答案
你不能在那里使用 if 但你可以使用三元运算符
for (int i = 0; i < str.length(); i += i%4 == 0 ? 1 : 3) {
result += str.charAt(i);
}
关于java - 在 For 循环增量中使用 if else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33416882/