Java - 将数组从数组方法返回到主函数

时间:2016-02-27 14:16:47

标签: java arrays methods return

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

IT SAYS:

incompatible types: String[] cannot be converted to String

PS: 不应修改主要功能。

7 个答案:

答案 0 :(得分:1)

您正在尝试将String数组分配给String

inputStudent(int i)  返回Array,但您尝试将Array分配给students[ i ] = inputStudent(i+1); 如 String [i]是元素String数组,它只接受String

答案 1 :(得分:1)

将您的方法更改为此。您不能将String数组分配给String。另外,为什么循环你的方法?如果我理解正确,你想把n个学生加入学生阵列?你循环进入每个学生的主要和方法运行。

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

答案 2 :(得分:1)

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

var fs = require('fs');
var https = require('https');

var privateKey  = fs.readFileSync('/etc/letsencrypt/live/gamepit.nl/privkey.pem', 'utf8');
var certificate = fs.readFileSync('/etc/letsencrypt/live/gamepit.nl/fullchain.pem', 'utf8');
var ca = fs.readFileSync('/etc/letsencrypt/live/gamepit.nl/chain.pem', 'utf8');

var credentials = {key: privateKey, cert: certificate, ca: ca};

var app = require('express')();

app.use(function(req, res, next) {
        console.log('site call!', req.originalUrl);
        next();
});

app.get('/', function(req, res) {
        res.send('Hello World');
        res.end();
});

var https = https.createServer(credentials, app);

https.listen(3000,'127.0.0.1',  function() {
        console.log('running!');
});

答案 3 :(得分:0)

你可以做的是改变:

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

答案 4 :(得分:0)

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

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

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

答案 5 :(得分:0)

我理解您要实现的内容,下面的代码将解决您的问题,每行代码都有解释。

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
}

答案 6 :(得分:-1)

您的代码中有错误。主

中不需要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;
}