【发布时间】:2019-02-26 20:40:21
【问题描述】:
我不知道为什么我的输出不正确。例如,如果输入是“跑步很有趣”,那么输出应该是“跑步很有趣”。但是,我得到的输出是“Iunning”。
import java.util.Scanner;
public class Problem1 {
public static void main( String [] args ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter text: ");
String sentence = sc.nextLine();
int space = sentence.indexOf(" ");
String firstWord = sentence.substring(0, space + 1);
String removedWord = sentence.replaceFirst(firstWord, "");
String newSentence = removedWord.substring(0,1).toUpperCase() +
firstWord.substring(1).toLowerCase();
System.out.println("");
System.out.println( newSentence );
}
}
【问题讨论】:
-
您的代码first在什么时候无法按您预期的方式工作?
-
@GBlodgett 分析得很好;为了让你朝着正确的方向前进,也许看看
String.split并在“”上拆分。
标签: java string uppercase lowercase