gpt4 book ai didi

Java-从数组方法返回一个数组到Main函数

转载 作者:行者123 更新时间:2023-11-30 03:02:14 27 4
gpt4 key购买 nike

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:main 函数不应修改。

最佳答案

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

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

关于Java-从数组方法返回一个数组到Main函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35670817/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com