Java:如何对填充有用户输入项目的 ArrayList 中的项目进行“排名”?

问题描述 投票:0回答:1

我正在创建一个 Java 程序,允许用户输入一组相关项目,然后将它们按照从大到小的顺序排列。代码的第一部分允许用户输入任意数量的项目,除非他们键入“STOP”,否则用户的输入将存储在 ArrayList 中。例如:

`请输入您想要添加到排名列表中的项目,或输入“停止”结束程序。

星球大战

请输入您想要添加到排名列表中的项目,或输入“停止”以结束该计划。

哈利·波特

请输入您想要添加到排名列表中的项目,或输入“停止”以结束该计划。

指环王

请输入您想要添加到排名列表中的项目,或输入“停止”以结束该计划。

停止`

预期结果应该是:

You have entered: Star Wars Harry Potter The Lord of the Rings

程序的这一部分已经完成,但我真的很难弄清楚下一步 - 实际排名。因为 ArrayList 中可以输入的内容没有限制,所以我不确定如何指示程序对输入的项目进行排名。我知道我想提示用户在输入的不同项目之间进行选择(“您更喜欢项目 A 还是项目 B?”),但我不确定当 ArrayList 没有可以输入的内容有限制。我可以得到一些指导吗?

`//Ranking.java是一个旨在提示用户相关项目列表的程序 //并将它们存储在数组中。然后程序会提示用户排名 //这些项目从最大到最小然后打印出它们的排名

//import Scanner and ArrayList
import java.util.*;

//declare class
public class Ranking {

    //declare main method
    public static void main(String[] args) {
        
        //initialize scanner
        Scanner input = new Scanner(System.in);
        
        //create ArrayList to store items
        ArrayList<String> items = new ArrayList<String>();
        
        //introduction
        System.out.println("Welcome to Ranking.java!");
        
        //while loop that repeats the prompt unless the user enters "STOP"
        while(true) {
            //prompt
            System.out.print("\nPlease enter an item you wish to add to the ranking list, "
                    + "or type 'STOP' to end the program.\n>> ");

            //stored in item variable
            String item = input.nextLine();
            
            //if item equals "STOP," program breaks and prints out the list of items entered
            if(item.equals("STOP")) {
                System.out.println("\nYou have entered:");
                for(int i = 0; i < items.size(); i++) {
                    System.out.println(items.get(i));
                }
                break;
            }
            //otherwise it adds to the ArrayList
            else {
                items.add(item);
            }
        }
        
        ArrayList<String> itemsRanked = new ArrayList<String>();
        
        //ranking introduction
        System.out.println("\nYou may now rank the following from greatest to least.\n");
        
        
    }
}
java sorting arraylist ranking
1个回答
0
投票

我修改了你的代码并添加了一些代码。这是我的测试结果之一。

Welcome to Ranking.java!

Please enter an item you wish to add to the ranking list, or type 'STOP' to end the program.
>> Item 1
Please enter an item you wish to add to the ranking list, or type 'STOP' to end the program.
>> Item 2
Please enter an item you wish to add to the ranking list, or type 'STOP' to end the program.
>> Item 3
Please enter an item you wish to add to the ranking list, or type 'STOP' to end the program.
>> stop

For item Item 1, enter a ranking from 1 to 3
>> 3
For item Item 2, enter a ranking from 1 to 3
>> 2
For item Item 3, enter a ranking from 1 to 3
>> 1

You ranked Item 3 as 1
You ranked Item 2 as 2
You ranked Item 1 as 3

我做的第一件事是创建一个

RankItem
类来保存
int
等级和
String
项目。

public class RankItem {
    
    private int rank;
    
    private final String item;

    public RankItem(String item) {
        this.item = item;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public String getItem() {
        return item;
    }
    
}

接下来,我将你的代码分为四种方法。第一个方法读取项目,第二个方法读取排名,第三个方法将

List
按排名、字母顺序排序,最后一个方法打印排序后的
List

分而治之。

如果用户输入多个相似的排名,则排序会按排名、字母顺序对列表进行排序。排名值可以是任何正值

int

这是完整的可运行代码。我将附加类设置为内部类,这样我就可以将代码作为一个块发布。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

// Ranking.java is a program designed to prompt user for a list of related items 
// and stores them in an array. The program will then prompt the users to rank 
// those items from greatest to least then prints out their ranking.

public class RankingItems {

    public static void main(String[] args) {
        RankingItems ri = new RankingItems();
        ri.processItems();
    }

    private final List<RankItem> rankItems;

    private final Scanner input;

    public RankingItems() {
        this.rankItems = new ArrayList<>();
        this.input = new Scanner(System.in);
    }

    public void processItems() {
        readItems();
        rankItems();
        sortItems();
        writeItems();
    }

    private void readItems() {
        System.out.println("Welcome to Ranking.java!");
        System.out.println();
        while (true) {
            // prompt
            System.out.println("Please enter an item you wish to "
                    + "add to the ranking list, "
                    + "or type 'STOP' to end the program.");
            System.out.print(">> ");
            String item = input.nextLine();
            if (item.equalsIgnoreCase("STOP")) {
                return;
            } else {
                rankItems.add(new RankItem(item));
            }
        }
    }

    private void rankItems() {
        System.out.println();
        for (RankItem rankItem : rankItems) {
            int rank = -1;
            while (rank < 0) {
                System.out.println("For item " + rankItem.getItem()
                        + ", enter a ranking from 1 to " + rankItems.size());
                System.out.print(">> ");
                String rankString = input.nextLine();
                try {
                    rank = Integer.valueOf(rankString);
                    rankItem.setRank(rank);
                } catch (NumberFormatException e) {
                    continue;
                }
            }
        }
    }

    private void sortItems() {
        Collections.sort(rankItems, new Comparator<RankItem>() {
            @Override
            public int compare(RankItem o1, RankItem o2) {
                if (o1.getRank() > o2.getRank()) {
                    return 1;
                } else if (o1.getRank() < o2.getRank()) {
                    return -1;
                } else {
                    return o1.getItem().compareTo(o2.getItem());
                }
            }
        });
    }

    private void writeItems() {
        System.out.println();
        for (RankItem rankItem : rankItems) {
            System.out.println("You ranked " + rankItem.getItem() + " as "
                    + rankItem.getRank());
        }
    }

    public class RankItem {

        private int rank;

        private final String item;

        public RankItem(String item) {
            this.item = item;
        }

        public int getRank() {
            return rank;
        }

        public void setRank(int rank) {
            this.rank = rank;
        }

        public String getItem() {
            return item;
        }

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.