我试图只在交换时大写第一个单词的第一个字母。仅交换第一个单词时首字母大写
这是我到目前为止。
import java.util.Scanner;
public class Ass2ID7239 {
public static void main(String[] args) {
System.out.println("Please enter a line of text without any punctuation:");
System.out.print("> ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
System.out.print("I have changed your text: ");
input.trim();
int firstSpace = input.indexOf(" ");
int lastSpace = input.lastIndexOf(" ");
String firstWord = input.substring(0, firstSpace + 1);
String middlePhrase = input.substring(firstSpace + 1, lastSpace);
String lastWord = input.substring(lastSpace, input.length());
lastWord = lastWord.toUpperCase();
System.out.println(lastWord + " " + middlePhrase + " " + firstWord);
}
}
你看到了什么问题?也就是说,你的输出(如果有的话)与你的预期有什么不同? – 2012-01-30 23:34:00
嗨,欢迎来到stackoverflow。我已经重申了你的问题,因为这与你的IDE没什么关系,更多的是使用字符串和(可能)空格。除非与问题相关,否则不必告诉开发工具集。 – 2012-01-30 23:45:44
当我编译然后运行时,它将整个第一个单词大写。我只希望它能够交换被交换的第一个单词的第一个字母。 – user1179184 2012-01-30 23:48:45