3

嗨,我是 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 语句处。我试图修复它但失败了。请问有什么建议可以解决这个问题吗?

4

5 回答 5

6
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];
于 2013-06-01T08:36:38.093 回答
2

一次只能返回一个值。不能返回多个值

创建一个包含您需要的多个值的类。在您的方法中,返回一个作为该类实例的对象。

这样,您仍然返回一个对象。在 Java 中,你不能返回一个以上的对象,不管它是什么。

public class TestVO {

    private char irstNameInitials;
    private char surnameInitials;
    public char getIrstNameInitials() {
        return irstNameInitials;
    }
    public void setIrstNameInitials(char irstNameInitials) {
        this.irstNameInitials = irstNameInitials;
    }
    public char getSurnameInitials() {
        return surnameInitials;
    }
    public void setSurnameInitials(char surnameInitials) {
        this.surnameInitials = surnameInitials;
    }    
}


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

   TestVO vo = new TestVO();
   vo.setIrstNameInitials(firstNameInitials);
    vo.setSurnameInitials(surnameInitials);

return vo; ;

}

于 2013-06-01T08:36:55.750 回答
0

请试试这个。你犯了一些简单的错误。

public class Testprg
{
   public void start()
   {
    String name = getNameFromUser();
    char firstNameInitials = getFirstNameInitials(name);
    char surnameInitials = getSurNameInitials(name);
    printInitials(firstNameInitials, surnameInitials);
   }

   private String getNameFromUser()
   {
    System.out.print("Please enter your name: ");
    String name = Keyboard.readInput();
    return name;
   }

   private char getFirstNameInitials(String name)
   {
    char firstNameInitials = name.charAt(0);

    return firstNameInitials;
   }

   private char getSurNameInitials(String name)
   {
    int indexOfSpace = name.indexOf(" ");
    char surnameInitials = name.charAt(indexOfSpace + 1);

    return surnameInitials;
   }

   private void printInitials(char firstNameInitials, char surnameInitials)
   {
    System.out.print("your initials are: " + firstNameInitials + ". " + surnameInitials + ".");
   }
}
于 2013-06-01T09:19:58.293 回答
0

错误:

return firstNameInitials; surnameInitials;

只返回一个值;

 printInitials(firstNameInitials, surnameInitials);

要么将函数转换为以 char 作为参数,要么将参数更改为字符串。

于 2013-06-01T08:40:52.297 回答
-1
public class ShowCurrentTime
{
    public static void main (String [] args)
    {
        //obtain the total milliseconds since Midnight, Sept 1, 2016
        long totalMilliseconds - System.currentTimeMillis();

        //total seconds
        long totalSeconds = totalMilliseconds / 1000;

        long currentSeconds = (int) (totalSeconds) % 60;

        //obtain total minutes
        long totalMinutes = totalSeconds / 60;

        //obtain current minute
        long currentMinute = (int) (totalMinutes) % 60;

        long toalHours = totalMinute / 60;

        long currentHour = (int) (totalHours % 24);

        System.out.println ("Current time is " + currenHour + ":" 
                                + currentMinute + " " + " " + currentSeconds 
                                + "GMT");
    }
}
于 2016-10-01T22:53:00.190 回答