java编程,输入一段英文字母,将每个单词的首字母转换为大写字母。
例如,Iamverygladtoseeyou的转换结果为IAmVeryGladToSeeYou这是我的编程:importjava.io.*;importjava.util...
例如,I am very glad to see you 的转换结果为 I Am Very Glad To See You
这是我的编程:
import java.io.*;
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()){
char c=(st.nextToken()).charAt(0);
if(c>='a'&&c<='z'){
c=(char)((int)c-32);
s1.append(c);
}
else
s1.append(c);
for(int i=1;i<(st.nextToken()).length();i++){
char c1=(st.nextToken()).charAt(i);
s1.append(c1);
}
System.out.print(" "+s1.toString());
}
}catch(IOException e){
System.out.println(e);
}
}
}
思路很乱,而且错了。
求修改!! 还有那个什么CSS我们没有学。。。
求助!!谢谢! 展开
这是我的编程:
import java.io.*;
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()){
char c=(st.nextToken()).charAt(0);
if(c>='a'&&c<='z'){
c=(char)((int)c-32);
s1.append(c);
}
else
s1.append(c);
for(int i=1;i<(st.nextToken()).length();i++){
char c1=(st.nextToken()).charAt(i);
s1.append(c1);
}
System.out.print(" "+s1.toString());
}
}catch(IOException e){
System.out.println(e);
}
}
}
思路很乱,而且错了。
求修改!! 还有那个什么CSS我们没有学。。。
求助!!谢谢! 展开
3个回答
展开全部
在你的基础上修改得
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
展开全部
// 构造方法
public Capitalized() {
}
public void printCapitalized() {
int i;
for (i = 0; i <cs.length; i++) {
if (Character.isSpaceChar(cs[i])){
i = i + 1;
cs[i]=Character.toUpperCase(cs[i]);
}
}
for (i = 0; i< cs.length; i++) {
System.out.print(cs[i]);
}
}
public static void main(String[] args) {
new Capitalized().printCapitalized();
}
}
//方法二
public class Capitalized {
public static void main(String[] args) {
String s="today is sunny!";
String arr[]=s.trim().split("\\s+");
if(s.length()>0)
for (int i = 0; i < arr.length; i++) {
arr[i]=Character.toUpperCase(arr[i].charAt(0))+arr[i].substring(1);
System.out.print(arr[i]+" ");
}
}
}
public Capitalized() {
}
public void printCapitalized() {
int i;
for (i = 0; i <cs.length; i++) {
if (Character.isSpaceChar(cs[i])){
i = i + 1;
cs[i]=Character.toUpperCase(cs[i]);
}
}
for (i = 0; i< cs.length; i++) {
System.out.print(cs[i]);
}
}
public static void main(String[] args) {
new Capitalized().printCapitalized();
}
}
//方法二
public class Capitalized {
public static void main(String[] args) {
String s="today is sunny!";
String arr[]=s.trim().split("\\s+");
if(s.length()>0)
for (int i = 0; i < arr.length; i++) {
arr[i]=Character.toUpperCase(arr[i].charAt(0))+arr[i].substring(1);
System.out.print(arr[i]+" ");
}
}
}
参考资料: http://zhidao.baidu.com/question/152789465
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
功能是实现了 ,有点繁琐,有待加工,除首字母外小写...
public class CharacterDemo {
public static void main(String[] args) {
String str = "goOd gooD stUdy dAy dAy up";
char[] c = str.toCharArray();
function (c);
str = String.copyValueOf(c);
System.out.println(str);
}
public static void function(char[] c){
for(int i = 0 ; i< c.length ; i++){
if(c[i] == ' '){
if(c[i+1]>='a' && c[i+1]<='z')
c[i+1] =(char)( c[i+1]-32 );
}else if(i==0){
if(c[i]>='a' && c[i]<='z')
c[i] =(char)( c[i]-32 );
}else if(c[i]>='A' && c[i]<='Z'&&c[i-1]!=' '){
c[i] = (char)(c[i]+32);
}
}
}
}
public class CharacterDemo {
public static void main(String[] args) {
String str = "goOd gooD stUdy dAy dAy up";
char[] c = str.toCharArray();
function (c);
str = String.copyValueOf(c);
System.out.println(str);
}
public static void function(char[] c){
for(int i = 0 ; i< c.length ; i++){
if(c[i] == ' '){
if(c[i+1]>='a' && c[i+1]<='z')
c[i+1] =(char)( c[i+1]-32 );
}else if(i==0){
if(c[i]>='a' && c[i]<='z')
c[i] =(char)( c[i]-32 );
}else if(c[i]>='A' && c[i]<='Z'&&c[i-1]!=' '){
c[i] = (char)(c[i]+32);
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询