【问题标题】:how do I resolve an incompatible types: return error in java with arrays?如何解决不兼容的类型:使用数组返回 java 中的错误?
【发布时间】:2020-12-02 15:32:42
【问题描述】:

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

主类:

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;
}

}

我的错误是:

PhoneBook.java:76: error: incompatible types: missing return value
  return;

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

另外,有没有办法缩短电话簿类中打印每个数组的 for 循环的行(46-75)?

谢谢!

【问题讨论】:

  • 我认为您对 return 语句的实际作用存在一些误解,因为您使用它的方式几乎没有意义。例如。当您立即从迭代中返回时,为什么要遍历您的 SD0 数组?这意味着它永远不会比第一次迭代更进一步,并且循环是毫无意义的。
  • 例如,如果您的代码到达return SD0[i]; 行,该方法将停止执行并返回该字符串值。一旦您的方法到达返回语句,这是您的方法将执行的最后一条语句。此外,如果您告诉编译器您的方法将返回一个值,它必须返回一个值,而您不能只做return;

标签: java arrays return-value return-type


【解决方案1】:

您在 print 方法中的最后一次返回什么也不返回,此时需要 String:

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

您可以考虑使用String [][] SD = new String[10][3]; 而不是将所有SDx 作为变量

【讨论】:

    【解决方案2】:

    return 语句立即返回。它不是在构建字符串。因此,只要 SD0 的长度 > 0,您的代码就会返回 SD0[0]。

    错误是完全没有参数的最终返回。 return "" 将修复编译错误,但它可能不是你真正想要的。

    【讨论】:

      猜你喜欢
      • 2011-08-26
      • 2021-04-15
      • 1970-01-01
      • 2020-12-05
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      相关资源
      最近更新 更多