java编程,输入一段英文字母,将每个单词的首字母转换为大写字母。
1个回答
展开全部
在你的基础上修改得
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class TTTest {
public static void main(String args[]) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
String s = in.readLine();
StringBuffer s1 = new StringBuffer();
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
String str = st.nextToken();
char c = (str).charAt(0);
if (c >= 'a' && c <= 'z') {
c = (char) ((int) c - 32);
s1.append(c);
} else
s1.append(c);
for (int i = 1; i < (str).length(); i++) {
char c1 = (str).charAt(i);
s1.append(c1);
}
s1.append(" ");
}
System.out.print(" " + s1.toString());
} catch (IOException e) {
System.out.println(e);
}
}
}
--------测试
i am a Student
I Am A Student
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class TTTest {
public static void main(String args[]) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
String s = in.readLine();
StringBuffer s1 = new StringBuffer();
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
String str = st.nextToken();
char c = (str).charAt(0);
if (c >= 'a' && c <= 'z') {
c = (char) ((int) c - 32);
s1.append(c);
} else
s1.append(c);
for (int i = 1; i < (str).length(); i++) {
char c1 = (str).charAt(i);
s1.append(c1);
}
s1.append(" ");
}
System.out.print(" " + s1.toString());
} catch (IOException e) {
System.out.println(e);
}
}
}
--------测试
i am a Student
I Am A Student
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询