名称

时间:2017-12-11 08:26:54

标签: java string

我想在代码中显示我的姓名缩写。例如,如果我的名字是Donald Trump,程序应该写D.T

我如何以最简单的方式做到这一点?

package pack_prov;
import javax.swing.*;

public class Filip_Degeryd_uppgift_3 {

    public static void main(String[] args) {
        String f;
        String e;

        f = JOptionPane.showInputDialog("first name ");            
        e = JOptionPane.showInputDialog("last name ");

        JOptionPane.showMessageDialog(null, "initials " + f + e + "is" +
                                           f.charAt(0) + ".") + e.charAt(0);
    }
}

1 个答案:

答案 0 :(得分:0)

下面:

public class Test {

    public static void main(String[] args) {

        Scanner scn = new Scanner(System.in);

        System.out.println("Enter First Name:");
        String frstNme = scn.nextLine();

        System.out.println("Enter Last Name:");
        String lstNme = scn.nextLine();

        System.out.println("Your Name Initial Is: " + frstNme.substring(0, 1) + "." + lstNme.substring(0, 1) + ".");

        scn.close();

    }

}