这是一项家庭作业,我们需要从用户那里输入他们的名字和姓氏,并输入空格。在输入这些数据后,用户按下Enter键并被要求输入他们的别名。一旦进入程序,输出用户输入的名字和姓氏,并添加“又名”和用户输入的别名,并且还以小写字母,大写字母以及首先和最后人物显示姓和名的首字母以全部小写字母命名。我已经成功地获得了程序显示名字和别名,以及首字母。但我无法弄清楚如何让最初的实例成为“AB”和“ab”实例,以及如何获取用户输入并将所有小写字母都写入。如何操纵一个字符串为大写和小写
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aliassearch;
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
/**
*
* @author students
*/
public class Aliassearch {
public static int user_input;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner user_input = new Scanner (System.in);
String first_name;
System.out.print("Please put in your first name followed by a space and a last name: ");
first_name = user_input.nextLine();
String s = new String(first_name);
int slength = s.length();
//System.out.println(slength); //
int sindex = s.indexOf(" ");
// System.out.println(sindex);
//alias parts
String alias_name;
System.out.print("Enter your alias: ");
alias_name = user_input.next();
// combines user input first name and alias name
String full_name;
full_name = first_name + " " + alias_name;
System.out.println(first_name + " aka " + alias_name );
//converts to initials
for (int i = 1; i < first_name.length(); i++) {
char c1 = first_name.charAt(i);
if (c1 == ' ') {
System.out.print(first_name.charAt(i - 7));
System.out.print(first_name.charAt(i + 1));
System.out.print(".");
String result;
result = s.toLowerCase();
}
}
}}
你是否研究过? :)在一秒钟内谷歌上有这么多的答案。对于一种解决方案,请查看http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toLowerCase()。我会让你考虑如何解决你的其他问题。提示,解决方案可能再次在'String'类中。 – nem035 2014-09-25 01:32:19