私のプログラムは機能していますが、私が望む方法ではありません。これは私が望む出力です:
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 '
このコードはそれをトリミングすると思いました:
if( tokens[k].length() > 1 ){
tokens[k] = tokens[k].trim();
}else{
tokens[k] = " ";
}
しかし、明らかにそうではありません。どうすればこれを達成できますか?