【问题标题】:Java-Returning an Array from an Array Method to the Main FunctionJava-从数组方法返回数组到主函数
【发布时间】:2016-06-10 19:06:15
【问题描述】:
public static public static void main(String[] args) {
    System.out.print("Enter the number of student names to input: ");
    int studentNum = new Scanner(System.in).nextInt();
    String students[ ] = new String[studentNum];
    for(int i = 0; i<studentNum; i++){
        students[ i ] = inputStudent(i+1);
    }
}

public static String[] inputStudent(int i) {
    String studentNum[] = new String[i];
    for(int a=0; a<i; a++) {
        System.out.print("Enter student name"+(a+1)+": ");
        studentNum[a]=new Scanner(System.in).nextLine();
    }
    return studentNum;
}

错误:

students[ i ] = inputStudent(i+1);

它说:

incompatible types: String[] cannot be converted to String

PS: 主函数不要修改。

【问题讨论】:

  • 你能解释一下 inputStudent 函数的用途吗?它不能返回学生数组,它应该读取一个学生并返回它。

标签: java arrays methods return


【解决方案1】:

你能做的就是改变:

students[ i ] = inputStudent(i+1);

students[ i ] = Arrays.toString(inputStudent(i + 1));

由于students 是一个字符串,您无法将数组直接分配给String。通过使用Array.toString(),它将以String 值的形式返回数组内容的表示形式。

再次查看您的问题,您无法修改主要内容。在这种情况下,我们可以将 inputStudent 设置为返回 String 类型。

public static String inputStudent( int i )
{
    String studentNum[] = new String[i];
    for( int a = 0; a < i; a++ )
    {
        System.out.print( "Enter student name" + (a + 1) + ": " );
        studentNum[a] = new Scanner( System.in ).nextLine();
    }
    return Arrays.toString(studentNum);
}

或短而甜。

public static String inputStudent( final int id )
{
    System.out.print("Enter student name at ID: "+id+": ");
    return new Scanner( System.in ).next();
}

【讨论】:

    【解决方案2】:

    修改您的输入学生方法,使其仅返回一个学生。

    public static String inputStudent(int i) {
        String studentNam = null;
        System.out.print("Enter student name"+i+": ");
        studentNum=new Scanner(System.in).nextLine();
        return studentNum;
    }
    

    【讨论】:

    • 你真的需要在这个函数中读取'n'个学生吗?为什么会这样?
    • 如果他不能修改main,那么他就不能从inputStudent返回一个String[],所以必须改变它。
    • 仍然不会用这种方式修改main方法。我猜他在这两种方法中都提出了错误的逻辑。只有一种方法内部应该有一个 for 循环。
    • 我同意 Python...正在确认您的答案。
    • 而且他不能在不修改 main 的情况下返回 'n' 名学生以使其工作:)
    【解决方案3】:

    我明白你要实现什么,下面的代码会解决你的问题,每一行代码都有解释。

    public static void main(String[] args) throws Exception {
        System.out.print("Enter the number of student names to input: ");
        Scanner scanner = new Scanner(System.in); // get the scanner
        int numOfStudents = scanner.nextInt();    // get the number of students from user
        String [] students = new String [numOfStudents];    // create an array of string
        int studentAdded = 0;   // counter
        scanner = new Scanner(System.in); // recreate the scanner, if not it will skip the first student.
        do {
            System.out.println("Enter student name "+(studentAdded+1)+": ");
            String studentName = scanner.nextLine();    // ask for student name
            students[studentAdded] = studentName;       // add student name to array
            studentAdded++;     // add 1 to the counter
        } while (studentAdded < numOfStudents); // loop until the counter become the same size as students number
        System.out.println("students = " + Arrays.toString(students)); // and show the result
    }
    

    【讨论】:

      【解决方案4】:

      由于您规定不能修改主函数,您将不得不将 inputStudent 函数的返回类型从 String[] 更改为 String。这意味着您必须更改在该函数中存储数据的方式。

      因此,将 String studentNum[] 更改为 String studentNum,而不是为数组的每个索引请求新的用户输入,而是一次请求所有输入,以逗号分隔并将它们存储在 studentNum 中。

      根据您的标准,这是一种可能的方法。

      【讨论】:

        【解决方案5】:

        把你的方法改成这个。您不能将字符串数组分配给字符串。另外,你为什么要循环你的方法?如果我理解正确,您想将 n 个学生添加到学生数组中?您为每个学生循环运行 main 和方法。

        public static String inputStudent(int i) {
                System.out.print("Enter student name");
                String studentName = new Scanner(System.in).nextLine();
                return studentName;
            }
        

        【讨论】:

          【解决方案6】:

          您的代码中有错误。 main中不需要for循环

          public static public static void main(String[] args) {
              System.out.print("Enter the number of student names to input: ");
              int studentNum = new Scanner(System.in).nextInt();
              String students[ ] = inputStudent(studentNum);
          }
          
          public static String[] inputStudent(int i) {
              String studentNum[] = new String[i];
              for(int a=0; a<i; a++) {
                  System.out.print("Enter student name"+(a+1)+": ");
                  studentNum[a]=new Scanner(System.in).nextLine();
              }
              return studentNum;
          }
          

          【讨论】:

            【解决方案7】:

            您正在尝试将字符串数组分配给字符串

            inputStudent(int i) 返回 Array,但您试图将 Array 分配给 students[ i ] = inputStudent(i+1); 作为 String[i] 是元素字符串数组,只接受字符串

            【讨论】:

              猜你喜欢
              • 2014-12-14
              • 2011-06-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多