关于JAVA输出单词首字母的程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于JAVA输出单词首字母的程序相关的知识,希望对你有一定的参考价值。
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.
这个程序该怎么写呢,谢谢!
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.");
参考技术A 用循环+数组+判断来实现,用循环把每个字母放到数组里,之后用循环逐个提取出数组的元素,如果为空格,则输出空格后面的字母,就得到首字母了。 参考技术B 就是简单的单词大小写转换和字符串截取而已。分三种情况:1个单词,2个目单词,3个目单词 参考技术C 首先确定单词是以空格间隔的。
输入后用空格分隔每个单词做成数组
循环数组,使用charat,获取第一个字母。
剩下的就是对数组的判断了。
Wrong. Please enter your names properly.
数组为1的情况吧。。。。
Variation One: NAVABI, Faye
Variation Two: Navabi, Faye
调换数组位置,String toUpperCase() ,toLowerCase()大小写 参考技术D 在workspace里可以有。
把一句话的每个单词首字母大写后输出
1 str = input(‘input a string you need to change:‘) 2 3 new_list = str.split(‘ ‘) 4 for i in new_list: 5 print(i.capitalize(),end=‘ ‘)
今天学字符串split用法的时候,突然想到前面str.capitalize()把字符串首字母大写
既然这样的话,就先把字符串通过空格分割没然后在把分割后的列表每个首字母大写
后来学习了 title 函数,发现python果然已经有了这种函数,直接 用就可以了
1 str = input(‘input a string you need ro change:‘) 2 print(str.title)
以上是关于关于JAVA输出单词首字母的程序的主要内容,如果未能解决你的问题,请参考以下文章