2016-03-13 117 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); 
} 
} 
相关问题