[英]How to change the words in a string and reprint the original string but with the new arrangement of words?
代碼:
String one, two, three,four, five,six,seven,eight;
Scanner kbrd = new Scanner(System.in);
System.out.println("please enter a phrase, and then pick three words that you want to switch");
one= kbrd.nextLine();
two= kbrd.next() +" ";
three = kbrd.next()+" ";
four = kbrd.next()+ " ";
two.toLowerCase();
three.toLowerCase();
four.toLowerCase();
seven = two+three+four;
System.out.println(one.indexOf(two));
System.out.println(one.lastIndexOf(four));
five = four.toUpperCase()+three+two.toUpperCase();
System.out.println(five);
eight = one.replace(seven.trim(),five.trim());
System.out.println(eight)
它可以工作,但是當有人輸入第二個用戶輸入的名稱為George時,它不會重新排列單詞以表示GEORGE IS NAME。 如果兩個用戶輸入均為“我的名字是喬治”,則該方法有效。 名字是喬治。 任何想法如何解決這一問題? 我的理論是,它無法在原語中找到它,因此它不知道要替換的內容,因為要替換的內容不是原始內容。
您的新代碼如下所示:
eight = one.replace(seven.trim(),five.trim());
// System.out.println(eight)
List<String> sentence = Arrays.asList(eight.trim().split(" "));
Collections.shuffle(sentence);
for(String str : sentence) {
System.out.println(str+" ");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.