如何解决一个不兼容的类型:使用数组在java中返回错误?

如何解决一个不兼容的类型:使用数组在java中返回错误?,java,arrays,return-value,return-type,Java,Arrays,Return Value,Return Type,这是我未完成的代码。我在phonebook类中有数组,main调用了一个方法来打印数组中的所有内容,但是出现了一个错误 主要类别: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); System.out.println("What would you li

这是我未完成的代码。我在phonebook类中有数组,main调用了一个方法来打印数组中的所有内容,但是出现了一个错误

主要类别:

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    
    Scanner myObj = new Scanner(System.in);
    System.out.println("What would you like to do: \n 1) ADD to phone book \n 2) DELETE from phone book \n 3) CALL \n 4) PRINT phone book \n 5) quit");
    int input = myObj.nextInt();


    //constructor scanner inputs
    String ISpeedDial = myObj.nextLine();
    String Iname = myObj.nextLine();
    String Inumber = myObj.nextLine();
    //constructor
    PhoneBook b1 = new PhoneBook(ISpeedDial, Iname, Inumber);

    if (input == 1) {//add contact
// scanner saying what is the name of your new contact? what is the # of you new contact? Would you like this contact to be on your spead dails. 
//ptints your new contat is:____ and their # is ___-___-____ and the your Spead dial number is_

    }
    else if (input == 2) {//delete contact
// 
    }
    else if (input == 3) {//make call(from contact or not?)

    }
    else if (input == 4) {//print phone book
    b1.print();
    System.out.println(b1.print() + " ");
    //System.out.print(SD0[i] + " ");
    //t1.Right();//gets right triangle t/f
    //System.out.println("It is a right triangle: " + t1.Right());

    }
    else if (input == 5) {//quit
      return;
    }
    else {//invalid number
      System.out.println("invalid input: " + input);
      return;
    }

  }
}
电话簿类别:

public class PhoneBook {

  //instance variables
  private String SpeedDial;
  private String name;
  private String number;

//Account constructor
public PhoneBook(String MSpeedDial, String Mname, String Mnumber){

SpeedDial = MSpeedDial;
name = Mname;
number = Mnumber;
}

//arrays
  String [] SD0 = new String[3];
  String [] SD1 = new String[3];
  String [] SD2 = new String[3];
  String [] SD3 = new String[3];
  String [] SD4 = new String[3];
  String [] SD5 = new String[3];
  String [] SD6 = new String[3];
  String [] SD7 = new String[3];
  String [] SD8 = new String[3];
  String [] SD9 = new String[3];

//method to deposit a specified amount into the account
public void add(){




  SD0[0] = "0";
  SD0[1] = "jo";
  SD0[2] = "123";

}

public void delete(){
  
}
//getter method to return balance
//named without void because it will return a value unlike the ones above
public String print(){
  for (int i = 0; i < SD0.length; i++) {  
    return SD0[i];  
  }
  for (int i = 0; i < SD1.length; i++) {  
    return SD1[i];  
  }
  for (int i = 0; i < SD2.length; i++) {  
    return SD2[i];  
  }
  for (int i = 0; i < SD3.length; i++) {  
    return SD3[i];
  }
  for (int i = 0; i < SD4.length; i++) {  
    return SD4[i];  
  }
  for (int i = 0; i < SD5.length; i++) {  
    return SD5[i];  
  }
  for (int i = 0; i < SD6.length; i++) {  
    return SD6[i];  
  }
  for (int i = 0; i < SD7.length; i++) {  
    return SD7[i]; 
  }
  for (int i = 0; i < SD8.length; i++) {  
    return SD8[i];  
  }
  for (int i = 0; i < SD9.length; i++) {  
    return SD9[i];  
  } 
  return;
}

}
有人知道如何解决这个问题吗

另外,是否有一种方法可以缩短phone book类中的行(46-75),即打印每个阵列的for循环


谢谢大家!

print方法中的最后一次返回不返回任何内容,而字符串应为:

  ...
  for (int i = 0; i < SD9.length; i++) {  
    return SD9[i];  
  } 
  return;
。。。
对于(int i=0;i

可以考虑使用<代码> String [][SD=新字符串[10 ][3 ];

而不是将所有的
SDx
作为变量

返回语句立即返回。这不是建立一个字符串。因此,只要SD0的长度大于0,代码就会返回SD0[0]


该错误是最终返回,没有任何参数。return“”将修复编译错误,但它可能没有达到您真正想要的效果。

我认为您对return语句的实际功能有一些误解,因为您使用它的方式毫无意义。e、 G.当您从迭代中立即返回时,为什么要在SD0数组上循环?这意味着它将永远不会超过第一次迭代,循环是毫无意义的
该方法将停止执行并返回该字符串值。一旦您的方法到达返回语句,该语句是您的方法将执行的最后一条语句。另外,如果您告诉编译器您的方法将返回一个值,那么它必须返回一个值,而您不能只执行
return
  ...
  for (int i = 0; i < SD9.length; i++) {  
    return SD9[i];  
  } 
  return;