继续使用if语句java

继续使用if语句java,java,if-statement,continue,Java,If Statement,Continue,为什么我不能使用continue with The?:接线员: public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (int i = 0; i < myList.length; i++) { System.out.println(myL

为什么我不能使用continue with The?:接线员:

public class TestArray {

public static void main(String[] args) {
  double[] myList = {1.9, 2.9, 3.4, 3.5};

  // Print all the array elements
  for (int i = 0; i < myList.length; i++) {
     System.out.println(myList[i] + " ");
  }


  // Finding the largest element
  double max = myList[0];
  for (int i = 1; i < myList.length; i++) {
     myList[i] > max ? max = myList[i] : continue ;
  }
  System.out.println("Max is " + max);  
}
} 
公共类测试阵列{
公共静态void main(字符串[]args){
double[]myList={1.9,2.9,3.4,3.5};
//打印所有数组元素
for(int i=0;imax?max=myList[i]:继续;
}
System.out.println(“最大值为”+Max);
}
} 

三元运算符不是这样工作的。它用于根据布尔表达式返回两个值中的一个

x = statement ? value1 : value2
如果这不是您想要的,那么使用一个简单的
If-else
语句。只需替换为:

for (int i = 1; i < myList.length; i++) {
    if(myList[i] > max)
        max = myList[i]
}

还有一点关于它在中的工作原理:

使用正常的
if
语句,因为三元运算符返回一个值。

三元运算符的工作方式如下:

public static <R> R ternaryOperator(boolean condition, R onTrue, R onFalse) {
    if (condition == true) {
        return onTrue;
    } else {
        return onFalse;
    }
}

这不是三元运算符的工作方式。它们旨在缩短条件赋值,而不是压缩代码路径。三元if需要返回值,而
continue
不提供返回值。因为该运算符需要两种情况下的表达式。Continue不是一个可以通过计算得到值的表达式!但是,条件运算符也不是语句表达式:即使您将表达式作为第三个操作数,该代码也不会编译。如果您不知道如何使用三元运算符并导致错误。建议是先学习,否则就使用if-else。max=myList[i]>max?myList[i]:max@数据科学家-这有助于你理解这个问题吗?
public static <R> R ternaryOperator(boolean condition, R onTrue, R onFalse) {
    if (condition == true) {
        return onTrue;
    } else {
        return onFalse;
    }
}
ternaryOperator(myList[i] > max, max = myList[i], continue)