Java初学者:找不到符号

时间:2013-03-27 09:20:58

标签: java

我一直在搜索java教科书几个小时试图确定我做错了什么。我得到的错误是第13行的“找不到符号”,这是代码行:

 System.out.println("The three initials are " + 
     getInitials(Harry, Joseph, Hacker));

说明在代码中注释。我很确定它与我设置的名字有关..但我不确定。

public class InitialsTest {
     /**
       Gets the initials of this name
      @params first, middle, and last names
      @return a string consisting of the first character of the first, middle,
  and last name
      */

    public static void main(String[] args) {
         System.out.println("The three initials are " + 
         getInitials(Harry, Joseph, Hacker));
    }

    public static String getInitials(String one, String two, String three) {
        String initials = one.substring(0,1) + two.substring(0,1) + three.substring(0,1);
        return initials;
    }

 }

5 个答案:

答案 0 :(得分:16)

System.out.println("The three initials are " 
    + getInitials("Harry", "Joseph", "Hacker")); //Enclosed within double quotes

这是您传递String文字的方式。

答案 1 :(得分:4)

System.out.println("The three initials are " + 
     getInitials("Harry", "Joseph", "Hacker"));

只需使用双引号即可。 如果你在代码中将它们声明为变量,则不需要双引号,

答案 2 :(得分:3)

你应该这样传递:

System.out.println("The three initials are " 
    + getInitials("Harry", "Joseph", "Hacker")); 
没有双引号(“”)的

Harry, Joseph, Hacker是可变的,并且您得到错误,因为您没有使用这些名称声明任何变量。

注意:Java 中的所有字符串必须双引号括起来。

答案 3 :(得分:2)

您有3个字符串值传递给getInitials(),字符串文字必须包含在"

System.out.println("The three initials are " + 
          getInitials("Harry", "Joseph", "Hacker"));

答案 4 :(得分:0)

字符串必须始终在"和"。所以你的代码将是

System.out.println("The three initials are " + 
 getInitials("Harry", "Joseph", "Hacker"));

另外,您也可以使用

String initials = one.charAt(0)+two.charAt(0)+three.charAt(0);

在你的getInitials()函数中而不是

String initials = one.substring(0,1) + two.substring(0,1) + three.substring(0,1);

只是说。两者都获得String中第0个索引位置的字符,但charAt返回为Character而不是String。