2016-03-13 123 views
0

我將製作一個應用程序,將輸入的單詞的首字母加蓋。我沒有收到任何錯誤,我只是沒有得到任何繼續運行的東西。我怎樣才能得到3個字的第一個字母?

package threeletteracronym; 

import java.util.Scanner; 

/** 
* 
* @author Matthew 
*/ 
public class ThreeLetterAcronym { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     String s; 

     Scanner keyboard = new Scanner(System.in); 

     System.out.println("Please enter words."); 
     s = keyboard.nextLine();  
     char a = keyboard.next().charAt(0); 
     a = Character.toUpperCase(a); 
     char b = keyboard.next().charAt(0); 
     b = Character.toUpperCase(a); 
     char c = keyboard.next().charAt(0); 
     c = Character.toUpperCase(a);   

     System.out.println("Your new Acronym form " + s + " is " + a + b + c);  
    } 

} 
+0

我假設這與'javascript'無關。 –

回答

1

您正在閱讀並放棄第一行輸入。

如果你不想這樣做,我建議你把此行s = keyboard.nextLine();

在這裏,如果您通過您的代碼步調試器會有所幫助。

0

你的代碼是不工作,因爲: 你需要刪除keyboard.nextLine()你犯了複製/粘貼錯字

b = Character.toUpperCase(a);而且必須是

b = Character.toUpperCase(b); 

例子:

System.out.println("Please enter words."); 
// s = keyboard.nextLine(); 
char a = keyboard.next().charAt(0); 
a = Character.toUpperCase(a); 
char b = keyboard.next().charAt(0); 
b = Character.toUpperCase(b); // uppercase of b and not a 
char c = keyboard.next().charAt(0); 
c = Character.toUpperCase(c); // uppercase of c and not a 
0

你可以這樣做:

import java.util.Scanner; 
public class test4 { 
public static void main(String[] args) { 
    @SuppressWarnings("resource") 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Please enter words."); 
    char a = keyboard.next().charAt(0); 
    a = Character.toUpperCase(a); 
    char b = keyboard.next().charAt(0); 
    b = Character.toUpperCase(a); 
    char c = keyboard.next().charAt(0); 
    c = Character.toUpperCase(a); 
    System.out.println("Your new Acronym form is:" + a + b + c); 
} 
} 

還有其他方法可以將每個字符保存到一個數組。然後您可以顯示該數組作爲結果。 這裏是通過使用字符串緩衝區:

import java.util.Scanner; 
public class test4 { 
public static void main(String[] args) { 
    @SuppressWarnings("resource") 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Please enter words: "); 
    char text; 
    StringBuffer sBuffer = new StringBuffer(5); 
    for(int i=0; i < 3; i++) { 
     text = keyboard.next().charAt(0); 
     text = Character.toUpperCase(text); 
     sBuffer = sBuffer.append(text); 
    } 
    System.out.println("Your new Acronym form is: " + sBuffer); 
} 
} 
相關問題