java - 使用后短路|

时间:2017-11-06 03:35:31

标签: java short-circuiting bitwise-or logical-or

我在dmoj上遇到了问题,它说有一个短路错误。

import java.util.*;
public class Main{
    public static void main (String[] args) {
       Scanner input = new Scanner(System.in);
       String[] ND = input.nextLine().split(" ");
       if(ND.length > 2) return;
       int[] ND_NUM = new int[ND.length];
       for(int i = 0; i < ND.length; i++){
         ND_NUM[i] = Integer.parseInt(ND[i]);
       }
       if(ND_NUM[0] < 1 | ND_NUM[0] > 10000) return;
       if(ND_NUM[1] < -10000 | ND_NUM[1] > 10000) return;
       String[] INTERVALS = input.nextLine().split(" ");
       int[] INTERVALS_NUM = new int[INTERVALS.length];
       for(int i = 0; i < INTERVALS.length; i++){
         INTERVALS_NUM[i] = Integer.parseInt(INTERVALS[i]);
       }

       int interval = ND_NUM[0];
       int timeToGo = ND_NUM[1];
       for(int i = 0; i < INTERVALS_NUM.length; i++){
         if(INTERVALS_NUM[i] == timeToGo/interval | INTERVALS_NUM[i] == (timeToGo/interval)*-1){
           System.out.print(INTERVALS_NUM[i]);
           return;
         }
       }
       System.out.print("This is not the best time for a trip.");
    }
}

我在另一篇文章中读到,为了防止短路错误,请使用单|代替||。但是,当我将if语句切换为使用单|时,它仍然给了我同样的错误。

0 个答案:

没有答案