gpt4 book ai didi

java - 如何将 'santa maria' 这样的字符串更改为 "Santa Maria' ?

转载 作者:行者123 更新时间:2023-11-30 04:47:29 24 4
gpt4 key购买 nike

我的程序正在运行,但不是按照我想要的方式运行。这是我想要的输出:

C:\Documents and Settings\Joe King\My Documents\418.85A Java\Projects\Day 6>java Project3 nina pinta "santa maria"



The Nina is decreasing throttle!

The Nina is ready to launch...

but the throttle is down, increase throttle!


The Pinta is lowering the main!

The Pinta is ready to launch...

but the sail is down, hoist the main!


The Santa Maria is hoisting the main!

The Santa Maria is ready to launch...

the sail is up, ahead full!



(press ENTER to exit)

如您所见,Santa Maria 已大写。这是我得到的输出:

C:\Documents and Settings\Joe King\My Documents\418.85A Java\Projects\Day 6>java Project3 nina pinta "santa maria"



The Nina is decreasing throttle!

The Nina is ready to launch...

but the throttle is down, increase throttle!


The Pinta is lowering the main!

The Pinta is ready to launch...

but the sail is down, hoist the main!


The santa maria is hoisting the main!

The santa maria is ready to launch...

the sail is up, ahead full!



(press ENTER to exit)

我的代码无法将 Santa Maria 等名称字符串大写。这是我的代码:

class Project3{

public static void main(String[] args){

Boat[] boatArray;
String result = " ";
char firstChar;
char firstLetter;
char secondLetter;
int i;

boatArray = new Boat[args.length];

if(args.length > 0){

for(i = 0 ; i < args.length ; i++){

String delimiters = "[ ]";
int limit = -1;

String[]tokens = args[i].split(delimiters, limit);

for( int k = 0 ; k < tokens.length ; ++k ){

if( tokens[k].length() > 1 ){

tokens[k] = tokens[k].trim();

}else{

tokens[k] = " ";

}

firstChar = tokens[k].charAt(0);

if(firstChar == ' '){

break;

}else{

if(Character.isUpperCase(firstChar)){

break;

}else{

firstChar = Character.toUpperCase(firstChar);
char[] tokenArray = tokens[k].toCharArray();
String text = new String(tokenArray, 1, (tokenArray.length - 1) );
tokens[k] = firstChar + text;

}

result = result + tokens[k];

if( k != tokens.length - 1 ){

break;

}else{

result = result.trim();
args[i] = result;
result = " ";

}
}
}

firstLetter = args[i].charAt(0);

if((firstLetter == 'B') || (firstLetter == 'C') || (firstLetter == 'N')){

boatArray[i] = new RaceBoat();
boatArray[i].christenBoat(args[i]);

}else{

boatArray[i] = new SailBoat();
boatArray[i].christenBoat(args[i]);

}


}

System.out.println("\n");

for(i = 0 ; i < args.length ; i++){

secondLetter = Character.toUpperCase(args[i].charAt(1));

if((secondLetter == 'A') || (secondLetter == 'E')){

boatArray[i].increaseSpeed();
boatArray[i].goFast();

}else{

boatArray[i].decreaseSpeed();
boatArray[i].goSlow();

}

boatArray[i].launchBoat();
boatArray[i].whatIsBoatState();

}

}else{

System.out.println("\n\nArgh!... you forgot to enter ship names scalawag!" +
"\n\n\n\tPlease try again!");

}

System.out.println("\n\n(press ENTER to exit)\n\n");

try{

System.in.read();

} catch(IOException e){

return;
}
}

}

认为问题可能是解析 santa maria 导致以下问题:

' santa '

' '

' maria '

我认为这段代码会修剪它:

if( tokens[k].length() > 1 ){

tokens[k] = tokens[k].trim();

}else{

tokens[k] = " ";

}

但显然事实并非如此。我该如何实现这个目标?

最佳答案

看一下 Commons-Lang StringUtils.capitalize 函数。

Capizalize

您已经获得了源代码,如果您想了解专业人士的制作方法,该源代码会很有用:)

这可能会使你的单词大写。

StringBuilder b = new StringBuilder();
String[] parts = originalWord.split(" ");
for(String part : parts) {
String capitalized = part.substring(0, 1).toUppercase() + part.substring(1);
b.append(capitalized).append(" ");
}
//and then you can remove the last space if it is your taste.
return b.toString();

它缺乏基本的检查,但它可以做到:)。

祝你好运!

关于java - 如何将 'santa maria' 这样的字符串更改为 "Santa Maria' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10629456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com