首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在java中打印任意长度的用户输入姓名的首字母和全名

在java中打印任意长度的用户输入姓名的首字母和全名
EN

Stack Overflow用户
提问于 2013-06-26 22:42:02
回答 2查看 8.6K关注 0票数 2

我是java新手,我一直在尝试解决一个问题,我觉得这个问题的答案可能比我的code.The问题更简单,那就是打印用户输入的任意长度姓名的首字母和完整的surname.But,这必须在没有任何String.split()或数组的情况下完成。我尝试让用户一次输入一个单词,但是否有任何可能的方法可以一次性获得完整的姓名并按要求执行操作。我的代码如下:

import java.io.*;
public class Initials {
    public static void main(String[]args)throws IOException{
        BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the number of words your name contains");
        int n=Integer.parseInt(br.readLine());
        String str="";
        for(int x=1;x<=n-1;x++){
            System.out.println("Enter your name's word number:"+" "+x);
            String s=br.readLine();
            String st=s.toUpperCase();
            char ch=st.charAt(0);
            str=str+ch+".";
        }
        System.out.println("Enter your surname");
        String sur=br.readLine();
        str=str+" "+sur.toUpperCase();
        System.out.println(str);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-06-23 17:17:54

另一种方式

import java.util.Scanner;
  //a class that will print your output
class Initial {
  public void Initials() {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Full name:");
    String name = sc.nextLine();
    int l = name.length();
    int pos = 0;
    for (int i = l - 1; i >= 0; i--) {
      char ch = name.charAt(i);
      if (ch == ' ') {
        pos = i; //getting the last space before Surname
        break;
      }
    }
    System.out.print("The initials are: ");
    System.out.print(name.charAt(0) + ".");//prints first name initial 
                                              //   with dot
    for (int x = 1; x < pos; x++) //finds midname initial
    {
      char ch = name.charAt(x);
      if (ch == ' ') {

        System.out.print(name.charAt(x + 1) + ".");
      }
    }
    for (int i = pos; i < l; i++) { //for printing Surname
      System.out.print(name.charAt(i));
    }

  }
}

public class Str {
  public static void main(String[] args) {
    Initial i = new Initial();
    i.Initials();
  }
}

票数 0
EN

Stack Overflow用户

发布于 2018-08-02 03:02:47

//此代码将适用于任何否。名称中的单词

class Surnam { 
    public static void main(String name) { 
        name = " " + name;
        int l=name.length(), p=0, m=0, r=0;
        char y;
        String word=" ", words=" ";
        for(int i = 0; i = 0; i--) {
            y=name.charAt(i);
            if(y==' ') { 
                r=name.lastIndexOf(y); //extracting the last space of the string
                word=name.substring(i,l); //extracting the surname
                words=name.replace(word," "); //removing the surname break;
            }
        }
        for (int i = 0; i <= r - 1; i++) {
            char x=words.charAt(i);
            if (x == ' ') {
                System.out.print(words.charAt(i + 1) + "."); //Printing all initials before the surname with a dot
            }
        }
        for (int i = l - 1; i >= 0; i--) {
            char x=name.charAt(i);
            if(x==' ') {
                m=i;
                name=name.substring(m,l); //extracting the surname
                name=name.trim(); //removing all the spaces before the surname
                System.out.print(name);
                break;
            }
        }
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17323254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档