简体   繁体   English

阅读三个单词并按字典顺序对其进行排序

[英]Reading three words and sorting them in lexicographic order

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; 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! :(请谢谢您的帮助!

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

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. 您在选择middle单词时缺少了else :第一个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; 
} 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM