关于JAVA输出单词首字母的程序

WriteacompleteJavaprograminasourcefiletobenamedAssignment3.java.Theprogramaskstheuser... Write a complete Java program in a source file to be named Assignment3.java.
The program asks the user for their full name (first middle last-using nextLine method) as one string in any combination of upper and lower case. Your task is to output the name in the following variations:
• Print just the initials in upper case letters
• Print the last name in upper case followed by a comma and the first name in lower case with the first letter capitalized and the middle initial capitalized followed by a period
• Print the last name comma first name and middle name – all names with first letter capitalized.
Note: Your program has to check for middle name and if the user does not have a middle name your program has to print the variations without the middle name. See the sample output below
Important: You will not get points if you do not read the string as one line (Use scan.nextLine()). You cannot use the Scanner class for extracting different parts of the name. You have to use the methods in class String

Here is the output your program should produce when the user enters the string shown in bold:
Sample Output: (the user enters the string shown in bold)

Sample output 1:
What are your first, middle, and last names?
david john smith
Your initials are: DJS
Variation one: SMITH, David J.
Variation two: Smith, David John

Sample output 2:
What are your first, middle and last names?
Faye Navabi
Your initials are: FN
Variation One: NAVABI, Faye
Variation Two: Navabi, Faye

Sample output 3:
What are your first, middle and last names?
Johnson
Wrong. Please enter your names properly.

这个程序该怎么写呢,谢谢!
展开
 我来答
B2K1bonPplR
推荐于2016-07-13 · TA获得超过2049个赞
知道小有建树答主
回答量:1156
采纳率:72%
帮助的人:396万
展开全部
import java.util.Scanner;
public class Assignment3 {
  public static void main(String[] args) {
    System.out.println("What are your first, middle, and last names?");
    Scanner scan = new Scanner(System.in);
    String fullName = scan.nextLine();
    if (fullName == null) {
      promptWrongName();
      return;
    }
    String[] names = fullName.split(" ");
    if (!verifyNames(names))
      return;
    printNames(names);
  }
  private static void printNames(String[] names) {
    printInitials(names);
    printVariationOne(names);
    printVariationTwo(names);
  }
  private static String capitalizeFirstLetter(String name) {
    return name.trim().substring(0, 1).toUpperCase() + name.trim().substring(1).toLowerCase();
  }
  private static String getInitialInUpperCase(String name) {
    return name.trim().substring(0, 1).toUpperCase();
  }
  private static void printVariationTwo(String[] names) {
    System.out.print("Variation Two: ");
    System.out.print(capitalizeFirstLetter(names[names.length - 1]));
    System.out.print(", ");
    System.out.print(capitalizeFirstLetter(names[0]));
    if (names.length == 3) {
      System.out.print(" ");
      System.out.print(capitalizeFirstLetter(names[1]));
    }
    System.out.println();
  }
  private static void printVariationOne(String[] names) {
    System.out.print("Variation One: ");
    System.out.print(names[names.length - 1].trim().toUpperCase());
    System.out.print(", ");
    System.out.print(capitalizeFirstLetter(names[0]));
    if (names.length == 3) {
      System.out.print(" ");
      System.out.print(getInitialInUpperCase(names[1]));
      System.out.print(".");
    }
    System.out.println();
  }
  private static void printInitials(String[] names) {
    System.out.print("Your initials are: ");
    for (int i = 0; i < names.length; i++) {
      System.out.print(getInitialInUpperCase(names[i]));
    }
    System.out.println();
  }
  private static boolean verifyNames(String[] names) {
    if (names.length != 2 && names.length != 3) {
      promptWrongName();
      return false;
    }
    for (int i = 0; i < names.length; i++) {
      if ("".equals(names[i].trim())) {
        promptWrongName();
        return false;
      }
    }
    return true;
  }
  private static void promptWrongName() {
    System.out.println("Wrong. Please enter your names properly.");
  }
}
dh3113shine
2013-09-18
知道答主
回答量:32
采纳率:0%
帮助的人:15.8万
展开全部
用循环+数组+判断来实现,用循环把每个字母放到数组里,之后用循环逐个提取出数组的元素,如果为空格,则输出空格后面的字母,就得到首字母了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
mfkvfn
2013-09-18 · TA获得超过197个赞
知道答主
回答量:125
采纳率:0%
帮助的人:80.7万
展开全部
就是简单的单词大小写转换和字符串截取而已。分三种情况:1个单词,2个目单词,3个目单词
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-09-18
展开全部
首先确定单词是以空格间隔的。

输入后用空格分隔每个单词做成数组
循环数组,使用charat,获取第一个字母。

剩下的就是对数组的判断了。
Wrong. Please enter your names properly.
数组为1的情况吧。。。。

Variation One: NAVABI, Faye
Variation Two: Navabi, Faye

调换数组位置,String toUpperCase() ,toLowerCase()大小写
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友957e987
2013-09-18
知道答主
回答量:71
采纳率:0%
帮助的人:24.7万
展开全部
在workspace里可以有。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式