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);
} 展开
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);
} 展开
7个回答
展开全部
//方法有很多种,这是你方法
//方法一
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]+" ");
}
}
}
//方法一
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]+" ");
}
}
}
参考资料: 还有其他问题的话,给我发百度消息
展开全部
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;
}
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]));
}
}
}
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]));
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以试试把这句话存到一个stringbuffer对象里,stringbuffer提供很多对字符串操作的方法
如果非要用char属于重复发明轮子
如果非要用char属于重复发明轮子
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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!”;
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!”;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询