2017-02-27 71 views
0

任何人都可以帮助我解决此问题,但我一直在收到错误消息。 我主要关心如何打印这些线路输出“打印出了名的长度如果超过10且小于20个字符长,并且在名称中第三个字母”打印出长度超过10个字符且长度小于20个字符的名称,并在名称中输入第三个字母

public static void main(String[] args) { 
    System.out.println("Class activity"); 

    Scanner scn = new Scanner(System.out); 
    System.out.println("Please enter your name"); 
    String name = scn.nextDouble(); 
    Main.processName(name); 
} 

/** 
* Print out the length of the name if it is more than 10 and less than 20 characters long, and the third letter in the name 
*/ 
private int processname(String name) { 
    int len = name.len(); 
    if (len > 10 | len < 20) { 
     System.out.println("Wow," + name + " that name is between 10 and 20 characters long!"); 
    } 
    char thirdLetter = name.charAt(3); 
    System.out.println("Your name has " + len + " letters and the second letter is " + thirdLetter); 
} 
+3

逻辑或是||,不是|。 –

回答

0

1)您使用或者在长度子句中代替AND。

2)单是没有错误,你只是得到所有长度为0 - 20的东西。但是错误是逻辑或是||。而不是|。同样逻辑AND是& &。

3)另外,字符串中的第三个字符是charAt(2),而不是charAt(3)。第一个字符的索引为0.

相关问题