将第一个单词更改为小写时,将第一个单词移至最后一个

时间:2019-03-13 21:36:39

标签: java string

用户输入“汽车为绿色”,输出为“汽车为绿色”。  输出应为“汽车为绿色”。我只是帮忙,我基本上完成了。如果可以让用户输入显示为粗体或带下划线,将很有帮助。

    public class MoveFirstWordToLast{
     public static void main(String[] args){
      Scanner keyboard = new Scanner(System.in);
      System.out.println("Enter a line of text. No punctuaton please");
      String sentence = keyboard.nextLine();

      int index = sentence.indexOf (' ');
      char word = sentence.charAt(index+1);

      //String change = String.valueOf(word).toLowerCase(); //uppercases the new word

      String start = String.valueOf(word).toUpperCase(); //uppercases the new word


      start = start + sentence.substring (index+2);
      start = start +" ";

      String end = sentence.substring (0 , index);
      System.out.println("I have rephrased that line to read: ");
      System.out.println(start + end);

    }
}

2 个答案:

答案 0 :(得分:0)

更改

String end = sentence.substring(0 , index);

String end = sentence.substring(0, index).toLowerCase();

答案 1 :(得分:0)

这是更新的代码。

git rev-list