输入一行文本。请不要加标点符号。
Java是这门语言。
我将这一行重新表述为:
是java语言。
尝试:
int x;
String sentence, first;
System.out.println("\nEnter a line of text. No punctuation please.");
Scanner keyboard = new Scanner (System.in);
sentence=keyboard.nextLine();
x = sentence.indexOf(" ");
first= sentence.substring(0,x);
second=sentence.substring(0,1)
second=second.toUpperCase();
System.out.println("I have rephrased that line to read:");
System.out.println(second+sentence.substring(x+1)+" "+first);输出:
输入一行文本。请不要加标点符号。
这是怎么回事
我将这一行重新表述为://它应该是“正在进行什么”
这是怎么回事?
附注:-I需要将字母"i“大写。如何使"second.substring(0,1)“读取字符"i"?按照建议,我试图弄清楚如何剥离字母并将其与大写字母连接起来,但我不确定。
致那些责备我不是自己做这件事的人。我已经多次尝试,并通读了这本书。这没有任何帮助。我不得不向教授请教,所以他给了我一个先机,但仍然不够。当然,我想要理解它。并不是每个人都以同样的方式掌握。就我个人而言,我需要例子和详细的解释来理解。
发布于 2011-02-01 01:50:55
更改:
first= sentence.substring(0,4); 至:
first= sentence.substring(0,x);然后添加:
sentence = sentence.substring(x+1,sentence.length());发布于 2011-02-01 01:51:37
尝试:
first = sentence.substring (0, x);
// other stuff
System.out.println (sentence.substring (x + 1) + " " + first);发布于 2011-02-01 01:53:27
TL;DR:使用另一个调用substring来剥离第一部分。请参阅the String class documentation和Manipulating Characters in a String
您的代码似乎根本不起作用。你在x中保存了第一个空格的位置,但是不要使用它,它应该是sentence.substring(0, x)。
对于句子: how are you。在first中,您将存储第一个单词: how
然后你应该去掉sentence中的第一个单词,比如:sentence = sentence.substring(x+1)。现在的句子将是:你是
在此之后,只需将两者连接起来即可:
String newSentence = sentence + " " + first;在这里,您已经存储在变量newSentence中:您是如何
愿上帝保佑!
https://stackoverflow.com/questions/4854149
复制相似问题