Java 阅读三个单词并按字典顺序排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19473430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading three words and sorting them in lexicographic order
提问by Derrick
I am trying to create a program that asks the User to type three words and sort them in lexicographic order.
我正在尝试创建一个程序,要求用户输入三个单词并按字典顺序对它们进行排序。
EXAMPLE;
例子;
- Enter three words separated by spaces:
- Pear Orange Apple
- Apple
- Orange
- Pear
- 输入以空格分隔的三个单词:
- 梨橙苹果
- 苹果
- 橘子
- 梨
The program is working fine (if I attempt the above example) except for one type of combination example that I will show below.
除了我将在下面展示的一种类型的组合示例外,该程序运行良好(如果我尝试上述示例)。
EXAMPLE;
例子;
- Enter three words separated by spaces:
- Orange Apple Pear
- Apple
- Pear
- Pear
- 输入以空格分隔的三个单词:
- 橙苹果梨
- 苹果
- 梨
- 梨
The program is skipping the first word (Orange) if it is supposed to appear in the middle of the three words.
如果第一个单词(橙色)应该出现在三个单词的中间,程序就会跳过它。
I believe that this line of code is affecting the program because it says that "this assigned value is never used" but I'm not sure how to fix it since I'm still an entry Java learner.
我相信这行代码正在影响程序,因为它说“这个分配的值从未使用过”,但我不知道如何修复它,因为我仍然是一个入门 Java 学习者。
- middle = firstWord;
- 中 = 第一个词;
Because of that line being unused, it's why Pear appeared twice.
由于该行未使用,这就是 Pear 出现两次的原因。
import java.util.*;
public static void main(String[] args)
{
Scanner wordInput = new Scanner(System.in);
String firstWord;
String secondWord;
String thirdWord;
System.out.println("Enter three words separated by spaces: ");
firstWord = wordInput.next();
secondWord = wordInput.next();
thirdWord = wordInput.next();
String top = firstWord;
String bottom = firstWord;
if( top.compareTo(secondWord) > 0)
{
top = secondWord;
}
if( top.compareTo(thirdWord) > 0)
{
top = thirdWord;
}
if( bottom.compareTo(secondWord) < 0)
{
bottom = secondWord;
}
if( bottom.compareTo(thirdWord) < 0)
{
bottom = thirdWord;
}
String middle;
if( !firstWord.equals(bottom) && !firstWord.equals(top) )
{
middle = firstWord;
}
if( !secondWord.equals(bottom) && !secondWord.equals(top) )
{
middle = secondWord;
}
else
{
middle = thirdWord;
}
System.out.println( top );
System.out.println( middle );
System.out.println( bottom );
}
}
Does anyone what I am missing or doing wrong? :( Please and thank you for any help!
有没有人我错过了什么或做错了什么?:( 请并感谢您的帮助!
采纳答案by dasblinkenlight
You are missing an else
in selecting the middle
word: the first if
correctly assigns Orange
to it, but then the last if
/else
re-assigns Pear
to it.
您else
在选择middle
单词时缺少一个:第一个if
正确分配Orange
给它,但最后一个if
/else
重新分配Pear
给它。
Here is how to fix your code:
以下是修复代码的方法:
if( !firstWord.equals(bottom) && !firstWord.equals(top) ) {
middle = firstWord;
} else if( !secondWord.equals(bottom) && !secondWord.equals(top) ) {
middle = secondWord;
} else {
middle = thirdWord;
}
回答by Trying
Rather than adding so much conditions you can simply do this by TreeSet:
您可以简单地通过TreeSet执行此操作,而不是添加这么多条件:
public static void main(String[] args) throws FileNotFoundException, IOException{
Scanner wordInput = new Scanner(System.in);
String firstWord;
String secondWord;
String thirdWord;
System.out.println("Enter three words separated by spaces: ");
firstWord = wordInput.next();
secondWord = wordInput.next();
thirdWord = wordInput.next();
TreeSet<String> treeSet=new TreeSet<>();
treeSet.add(firstWord);
treeSet.add(secondWord);
treeSet.add(thirdWord);
for(String s:treeSet){
System.out.println(s);
}
}
And if you have duplicate:
如果你有重复:
Map<String, Integer> treeMap=new TreeMap<>();
if(treeMap.containsKey(firstWord)){
Integer i=treeMap.get(firstWord);
i++;
treeMap.put(firstWord, i);
}
else{
treeMap.put(firstWord, 1);
}
if(treeMap.containsKey(secondWord)){
Integer i=treeMap.get(secondWord);
i++;
treeMap.put(secondWord, i);
}
else{
treeMap.put(secondWord, 1);
}
if(treeMap.containsKey(thirdWord)){
Integer i=treeMap.get(thirdWord);
i++;
treeMap.put(thirdWord, i);
}
else{
treeMap.put(thirdWord, 1);
}
for(String s:treeMap.keySet()){
int k=treeMap.get(s);
while(k>0){
System.out.println(s);
k--;
}
}
回答by Moh Kreifeur
Here is another solution with less code
这是另一个代码较少的解决方案
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Enter three words separated by spaces: ");
//Scanner wordsInput = new Scanner(System.in);
Console console = System.console();
//List<String> words = new ArrayList<>();
List<String> words = Arrays.asList(console.readLine().split("\s+"));
Collections.sort(words);
for (String word : words) {
System.out.println(String.format("- %s",word));
}
}