Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 缺少返回语句:try catch_Java_Oop_Methods_Return_Try Catch - Fatal编程技术网

Java 缺少返回语句:try catch

Java 缺少返回语句:try catch,java,oop,methods,return,try-catch,Java,Oop,Methods,Return,Try Catch,try和catch块中有一个return语句,但仍出现错误: 缺少返回语句。 我用的是BlueJ顺便说一句 public int[] getInput(){ boolean rightInput = false; int[] nothing = {0,0}; while(rightInput == false){ try{ rightInput = true; int[] XY = {0,0}; String coordi

try和catch块中有一个return语句,但仍出现错误: 缺少返回语句。 我用的是BlueJ顺便说一句

public int[] getInput(){
    boolean rightInput = false;
    int[] nothing = {0,0};
    while(rightInput == false){
      try{
       rightInput = true;
      int[] XY = {0,0};
      String coordinates;
      System.out.println("Insert coordinates (x y) and press enter.");
      coordinates = input.nextLine();
      XY[1] = Character.getNumericValue(coordinates.charAt(0));
      XY[0] = Character.getNumericValue(coordinates.charAt(2));
      return XY;
     }catch(StringIndexOutOfBoundsException se){
      System.out.println(se);
      System.out.println("Try Again with a space between x and y.");
      rightInput =  false;  

      return nothing;
     }

    }

}

while
循环后必须有一个return语句,因为控件可能根本不进入while循环体(当
rightInput
为true时),1在这种情况下,方法必须返回一些内容

移动
不返回任何内容之后,while块将为您工作

或者,您可以将while循环条件设置为
true

while (true) {
  try {
    ...
    return XY;

  } catch(...) {
    ...
    return nothing;
  }

}


1尽管在您的情况下,
rightInput
始终为false,但编译器不会推断(可能不能?),这是一件好事。想想如果有专门的逻辑来动态计算
rightInput
,会发生什么。然后将导致编译错误,强制添加返回语句,因为编译器现在无法判断是否将执行while循环体。

如果您的代码不满足while循环中指定的条件,因此不进入while循环,该怎么办?因此,为了涵盖该场景,while循环之外也应该有一个return语句。放

return null
就在方法结束之前

while{
  try {

  }
  catch {

  }
}
return null;

将代码更改为:

 public class JavaAPP {

         public int[] getInput(){
        boolean rightInput = false;
        int[] nothing = {0,0};
        while(rightInput == false){
          try{
           rightInput = true;
          int[] XY = {0,0};
          String coordinates;
          System.out.println("Insert coordinates (x y) and press enter.");
          coordinates = input.nextLine();
          XY[1] = Character.getNumericValue(coordinates.charAt(0));
          XY[0] = Character.getNumericValue(coordinates.charAt(2));
          return XY;
         }catch(StringIndexOutOfBoundsException se){
          System.out.println(se);
          System.out.println("Try Again with a space between x and y.");
          rightInput =  false;  

          return nothing;
         }

        }
             return null;

    }  
        public static void main(String[] args) {

    JavaAPP Obj = new JavaAPP (); 
    Obj.getInput(); 

    }

    }

我从您的代码中了解到,您希望在while循环中执行一个操作,直到rightInput变为true,如果其间发生任何错误,则不应中断循环

您所做的是在每次循环执行后返回值。其中,return本身将使您脱离循环,为此,我们使用break操作符

我建议您将返回XY移出循环。请在下面查找更新代码:

    public int[] getInput(){
       boolean rightInput = false;
       //int[] nothing = {0,0}; not required any more
       int[] XY = {0,0}; // moved outside loop
       while(rightInput == false){
       try{
            rightInput = true;
            String coordinates;
            System.out.println("Insert coordinates (x y) and press enter.");
            coordinates = input.nextLine();
            XY[1] = Character.getNumericValue(coordinates.charAt(0));
            XY[0] = Character.getNumericValue(coordinates.charAt(2));
       } catch(StringIndexOutOfBoundsException se) {
            System.out.println(se);
            System.out.println("Try Again with a space between x and y.");
            rightInput =  false;

           // return nothing; not required any more it is same as XY
        }

    }
    return XY;
    }

在while循环下添加'returnnothing'后,它编译正确。非常感谢。