我完成了按字母顺序排列单词的程序,但我试图按字母顺序排列整个句子……或段落……或故事。几乎你扔给它的任何东西。
到目前为止,这是我的代码:
package Main;
import java.io.Console;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class ABCs2
{
public static final void main(String args[])
{
Scanner a = new Scanner(System.in);
char reply;
String r1, r2, r3, r4;
String s1;
System.out.print("Alphabetize words: W | or press S to sort sentence..");
reply = a.findWithinHorizon(".", 0).charAt(0);
if (reply == 'W' || reply == 'w')
{
System.out.print("Enter word 1");
r1 = a.next();
System.out.print("Enter word 2");
r2 = a.next();
System.out.print("Enter word 3");
r3 = a.next();
System.out.print("Enter word 4");
r4 = a.next();
List<String> list = new ArrayList<>();
list.add(r1);
list.add(r2);
list.add(r3);
list.add(r4);
System.out.println("Alphabetizing...");
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
java.util.Collections.sort(list);
System.out.println("Your alphabetized list:");
System.out.println(list);
for (String str : list)
{
System.out.println(str);
}
}
if (reply == 'S' || reply == 's')
{
System.out.println("Enter sentence...");
s1 = a.next();
List<String> slist = new ArrayList<>();
slist.add(s1);
System.out.println("Alphabetizing...");
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
java.util.Collections.sort(slist);
System.out.println("Your alphabetized sentence...");
System.out.println(slist);
}
}
}
我想输出整个按字母顺序排列的句子,但我只得到第一个单词。我什至需要使用列表吗?谢谢!我不需要它完全回答,只是朝着正确的方向迈出一步!