这是我未完成的代码。我在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;
}
}
我的错误是:
PhoneBook.java:76: error: incompatible types: missing return value
return;
有人知道如何解决这个问题吗?
另外,有没有一种方法可以缩短phone book类中的行(46-75),即打印每个数组的for循环?
谢谢您!
2条答案
按热度按时间qnyhuwrf1#
return语句立即返回。它不是在建一根绳子。所以只要sd0的长度大于0,代码就会返回sd0[0]。
错误是没有任何参数的最终返回。return“”将修复编译错误,但它可能不是您真正想要的。
vngu2lb82#
print方法中的最后一个返回值不返回任何内容,但需要字符串:
你可以考虑使用
String [][] SD = new String[10][3];
而不是拥有SDx
作为变量