gpt4 book ai didi

Java 编译器错误 : not a statement

转载 作者:行者123 更新时间:2023-12-03 18:11:11 32 4
gpt4 key购买 nike

大家好,我是 Java 编程的新手。我想编写一个 java 程序来获取用户的名字和姓氏首字母。我的代码是:

public class MyProgram {
public void start() {
String name = getNameFromUser();
char firstNameInitials = getInitials(name);
char surnameInitials = getInitials(name);
printInitials(firstNameInitials, surnameInitials);
}
private String getNameFromUser() {
System.out.print("Please enter your name: ");
String name = Keyboard.readInput();
return name;
}
private char getInitials(String name) {
char firstNameInitials = name.charAt(0);
int indexOfSpace = name.indexOf(" ");
char surnameInitials = name.charAt(indexOfSpace + 1);

return firstNameInitials; surnameInitials;
}

private void printInitials(String firstNameInitials, String surnameInitials){
System.out.print("your initials are: " + firstNameInitials +". " + surnameInitials +".");
}
}

我收到一条错误消息,我不知道它是什么意思:

Error: not a statement.

错误似乎在第 18 行,在 return 语句处。我试图修复它但失败了。请问有什么建议可以解决这个问题吗?

最佳答案

return firstNameInitials; surnameInitials;

你不能像这样返回多个字符。而是使用 char 数组返回

比如你可以这样返回

private char[] getInitials(String name) {
char firstNameInitials = name.charAt(0);
int indexOfSpace = name.indexOf(" ");
char surnameInitials = name.charAt(indexOfSpace + 1);

char[] result = {firstNameInitials, surnameInitials};
return result;
}

并称它为

char[] a = getInitials(name);
char firstNameInitials = a[0];
char surnameInitials = a[1];

关于Java 编译器错误 : not a statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16870788/

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