java变量类型问题

发布于 2024-10-08 10:31:41 字数 1218 浏览 0 评论 0原文

所以这是我的问题,我试图在数组中搜索值,然后返回索引和字符串名称。

但我的问题是,当我返回索引时,它将其设置为字符串,因为这就是该方法所要求的,因此当我尝试将索引更改为字符串时,我的编译器会抛出错误。这是一些方法的麻烦,如果你们想看的话我可以发布我的整个代码。

public static String studentGrade(Scanner stdIn)
{
    System.out.print("What student do you want to enter a grade for?");
    String searchName=stdIn.next();
    String index = findStudent(students, searchName);
    int studentId =(int) currentStudentId(students, searchName);

    if (index == searchName) 
    {    
        System.out.println("Student was found, " + index + studentId);
        System.out.println("What is the grade that you want to input for this student?");
        String input = stdIn.next();
        String studentGrade = input;
        return studentGrade;
    }    
    else 
    {    
        System.out.println("not found"); 
    }
}

public static String findStudent(String[] students, String searchName)
{
    for (int i = 0; i < students.length; i++)
    {
        if (students[i].equals(searchName))
        {
            String currentStudentId = i;
            return currentStudentId;
            return searchName;
        }
    } // end for 
    String fail = "fail";
    return fail;
} // end findStudent

So here is my problem im trying to search an array for a value and then return the index as well as the string name.

But my problem is that when i return the index it sets it as a string because thats what the method called for so when i try to type cast the index to be changed to a string my compiler throws errors. Here are the trouble some methods, i can post my whole code if you guys would like to see it.

public static String studentGrade(Scanner stdIn)
{
    System.out.print("What student do you want to enter a grade for?");
    String searchName=stdIn.next();
    String index = findStudent(students, searchName);
    int studentId =(int) currentStudentId(students, searchName);

    if (index == searchName) 
    {    
        System.out.println("Student was found, " + index + studentId);
        System.out.println("What is the grade that you want to input for this student?");
        String input = stdIn.next();
        String studentGrade = input;
        return studentGrade;
    }    
    else 
    {    
        System.out.println("not found"); 
    }
}

public static String findStudent(String[] students, String searchName)
{
    for (int i = 0; i < students.length; i++)
    {
        if (students[i].equals(searchName))
        {
            String currentStudentId = i;
            return currentStudentId;
            return searchName;
        }
    } // end for 
    String fail = "fail";
    return fail;
} // end findStudent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-10-15 10:31:41

我不认为将 String 和索引都返回为 int 是一个好主意。为什么不直接返回索引并通过在包含所有学生的数组中查找来获取学生。
如果未找到该学生,您可以返回 -1。

这就是我的意思:

String[] students = new String[] { ... };

//...

int indexFound = findStudent(students, "John Doe");
if (indexFound >= 0) {
   String studentFound = students[indexFound];
}

PS。您的代码包含错误(例如双重 return 命令)

I don't believe returning both String and index as int is a good idea. Why don't you just return index and get the student by looking up in array that contains all students.
If the student wasn't found you can return for example -1.

This is what I mean:

String[] students = new String[] { ... };

//...

int indexFound = findStudent(students, "John Doe");
if (indexFound >= 0) {
   String studentFound = students[indexFound];
}

PS. Your code contains errors (like doubled return command)

疑心病 2024-10-15 10:31:41

为什么您希望将 searchNamefindStudent() 返回到通过参数传递它的方法。

当然,调用者方法已经具有该值。只需返回索引:

示例:

public static int findStudent(String[] students, String searchName)
{
    for (int i = 0; i < students.length; i++)
    {
        if (students[i].equals(searchName))
        {
            return i;
        }
    } // end for 
    int fail = -1;
    return fail;
} // end findStudent

Why would you want to return searchName from findStudent() to a method that passes it through an argument.

Of course the caller method already has the value. Just return the index:

Example:

public static int findStudent(String[] students, String searchName)
{
    for (int i = 0; i < students.length; i++)
    {
        if (students[i].equals(searchName))
        {
            return i;
        }
    } // end for 
    int fail = -1;
    return fail;
} // end findStudent
在风中等你 2024-10-15 10:31:41

索引自然是一个int,而不是一个String。你应该返回一个int。

假设这是家庭作业,并且您必须返回一个字符串,您可以使用以下命令将数字转换为字符串。

return ""+currentStudentId;

但是,您遇到的问题是您试图返回两个值。

我怀疑你对要求的理解有误,建议你再看一遍。

一个更短的示例,使用 varargs

public static int findString(String string, String... strings) {
    for (int i = 0; i < strings.length; i++)
        if (strings[i].equals(string))
            return i;
    return -1; // for not found.
}

或者甚至以下适用于任何类型。

public static <T> int indexOf(T t, T... ts) {
    for (int i = 0; i < ts.length; i++)
        if (ts[i].equals(t))
            return i;
    return -1; // for not found.
}

例如

int found = indexOf(5, 1,3,5,7); // found = 2;

An index is naturally an int, not a String. You should return an int.

Assuming this is homework, and you have to return a String, you can convert a number into a String using the following.

return ""+currentStudentId;

However, the problem you have is that you are trying to return two values.

I suspect you have mis-understood the requirements, I suggest you read them again.

A shorter example, using varargs

public static int findString(String string, String... strings) {
    for (int i = 0; i < strings.length; i++)
        if (strings[i].equals(string))
            return i;
    return -1; // for not found.
}

Or even the following works for any type.

public static <T> int indexOf(T t, T... ts) {
    for (int i = 0; i < ts.length; i++)
        if (ts[i].equals(t))
            return i;
    return -1; // for not found.
}

e.g.

int found = indexOf(5, 1,3,5,7); // found = 2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文