在将第一个单词更改为小写的同时将第一个单词移动到最后

问题描述 投票:-1回答:2

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

    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);

    }
}
java string
2个回答
0
投票

这是更新的代码。

import java.util.Scanner;

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 +" ";
      System.out.println(start);
      String end = sentence.substring (0 , index);
      end = end.toLowerCase();
      System.out.println("I have rephrased that line to read: ");
      System.out.println(start + end);

    }
}

0
投票

更改

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

String end = sentence.substring(0, index).toLowerCase();
© www.soinside.com 2019 - 2024. All rights reserved.