用户输入“汽车为绿色”,输出“汽车为绿色”。输出应该是“汽车是绿色的”。我只是帮忙,我基本上完成了。如果可以将用户输入显示为粗体或带下划线,将会很有帮助。
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);
}
}
这是更新的代码。
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);
}
}
更改
String end = sentence.substring(0 , index);
至
String end = sentence.substring(0, index).toLowerCase();