Java问题 怎么将将一个英文句子中的每一个单词的首字母转化为大写字母?急~~~~~!!!

完全不在状态,这是我编的,一堆错误。。。。财富值没了,一共就6个。。。Capitalized.javapublicclassCapitalized{chara=newch... 完全不在状态,这是我编的,一堆错误。。。。财富值没了,一共就6个。。。
Capitalized.java
public class Capitalized{
char a = new char[13];
char[13] a = {"T","o","d","a","y"," ","i","s"," ","s","u","n","n","y","!"};

//构造方法
public Capitalized(){

}

//get和set方法
public void setCapitalized(String[] a){

this.a = a;
}

public String getCapitalized(){
return a;
}

public char printCapitalized(){
int i;
for(i=0;i<=13;i++){
boolean Character.isSpaceChar(char[i] a);
if (char[i] a == " ")
i=i+1;
char Character.toUpperCase(char[i] a);
}

}

}
CapitalizedTest.java
import java.util.*;

public class CapitalizedTest{
public static void main(String[] args){

char[13] a = {"T","o","d","a","y"," ","i","s"," ","s","u","n","n","y","!"};
String s = new String(a);
Capitalized c = new Capitalized();

System.out.println("Enter a string > Today is sunny!")

c.setCapitalized(s);
s = c.getCapitalized();
System.out.println(s);
}
展开
 我来答
百度网友5ca104357
推荐于2017-11-23 · TA获得超过2595个赞
知道小有建树答主
回答量:874
采纳率:100%
帮助的人:1375万
展开全部
//方法有很多种,这是你方法
//方法一
public class Capitalized {
char[] cs = { 'T', 'o', 'd', 'a', 'y', ' ', 'i', 's', ' ', 's', 'u', 'n',
'n', 'y', '!' };

// 构造方法
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]+" ");
}
}

}

参考资料: 还有其他问题的话,给我发百度消息

7740798
2010-05-13 · TA获得超过110个赞
知道答主
回答量:89
采纳率:0%
帮助的人:88.8万
展开全部
String s="today is sunny!";
public String toUppString(String str)
{
String arg[]=str.split(" ");
String s="";
for(int i=0;i<arg.length;i++)
{
s+=arg[i].substring(0,1).toUpperCase()+arg[i].substring(1);
}

return s;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2010-05-13
展开全部
根据要求写了一段小程序,希望对你有帮助

import java.util.*;

public class Capitalized {

/*
* Capital one word
*/
public String capitalOneWord(String word) {
if (word == null)
return null;

//get the upper case for first character, then add the rest
return word.substring(0, 1).toUpperCase() + word.substring(1);
}

/*
* Capital one sentence
*/
public String capitalOneSentence(String sentence) {
if (sentence == null)
return null;

StringTokenizer st = new StringTokenizer(sentence);
StringBuffer result = new StringBuffer();

//devide sentence by word, process with capitalOneWord() once a time
while (st.hasMoreTokens()) {

result.append(capitalOneWord(st.nextToken())+" ");

}
return result.toString();
}

public static void main(String[] args) {

String[] test = { "Today is Sunndy, how aboout tomorrow?",
"How are you, darling",
"hi, sweat heart~",
"",
null
};

Capitalized tool = new Capitalized();

for(int i=0;i<test.length;i++){
System.out.println(tool.capitalOneSentence(test[i]));
}

}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友c29bfb6f2
2010-05-13 · TA获得超过7521个赞
知道大有可为答主
回答量:6020
采纳率:0%
帮助的人:0
展开全部
你可以试试把这句话存到一个stringbuffer对象里,stringbuffer提供很多对字符串操作的方法

如果非要用char属于重复发明轮子
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
呆舟
2010-05-15
知道答主
回答量:11
采纳率:0%
帮助的人:4.7万
展开全部
printCapitalized()函数有问题啊。。。。
public char printCapitalized(String s){
char[] a=s.toCharArray();
for(int i=0;i<=13;i++){
if (a[i] == ' ')
i=i+1;
char Character.toUpperCase(a[i]);
}
}
刚开始的这两句也有问题,应该删掉:
char a = new char[13];
char[13] a = {"T","o","d","a","y"," ","i","s"," ","s","u","n","n","y","!"};
还有主函数里的这两句也对,可是多余的呀
char[13] a = {"T","o","d","a","y"," ","i","s"," ","s","u","n","n","y","!"};
String s = new String(a);
直接String s=“Today is sunny!”;
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式