2017-02-27 38 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.

相關問題