我需要使用标题,开发人员,类型等对游戏进行排序 [英] I need to sort games in an array using title, developer, genre, etc

查看:88
本文介绍了我需要使用标题,开发人员,类型等对游戏进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何对数组做很多事情,还无法弄清楚.这是完整的作业.

  1. 编写一个JAVA程序,该程序使用数组维护计算机游戏的列表.您的主程序应重复显示以下菜单.

i.插入游戏

s.搜索游戏

p.打印列表

q.退出

选择:

该数组存储一个计算机游戏列表,列表中的每个游戏都包括标题(字符串,键),开发者(字符串),类型(字符串),生产年份(整数)和价格(浮动).该列表应按键(标题)的升序进行维护.

选项i应该阅读游戏(标题,开发人员,类型,年份,价格),然后将游戏插入数组.请注意,应将新游戏插入正确的位置,以便整个数组可以保持排序.在数组末尾添加新游戏后再次对整个数组进行排序是昂贵的,因此是不可接受的. Option s要求提供游戏标题,并列出与输入的标题匹配的所有游戏.请注意,两个或多个游戏可能具有相同的标题.选项p仅列出存储在数组中的所有游戏.

我们最多假设100场比赛.

这是我到目前为止所拥有的

包lab12;

 import java.util.Scanner;

  // create second class to hold methods
class next {

// create "insert game" method
public void insert( ) {
    System.out.println("Insert");

}

// create "search game" method
public void search( ) {
    System.out.println("Search");

}

// create "print list" method
public void print( ) {
    System.out.println("Print");

}

// create method to 

  }

 public class Lab12 {

public static void main(String[] args) {
    Scanner in = new Scanner( System.in );

    // create string to see what user wishes to do
    String choose;

    // create instance of other class
    next choice = new next();

    do {
        // see which method user wants to use
    System.out.print("Do you want to insert game (i) or search game (s) "
            + "or print list (p) or quit (q)? ");
    // create string to see which method to go to
    choose = in.nextLine();

    // send user to correct method
    if ( choose.equals("i") || choose.equals("I"))
        choice.insert();
    else if ( choose.equals("s") || choose.equals("S") )
        choice.search();
    else if ( choose.equals("p") || choose.equals("P") )
        choice.print();

    } while ( choose.equals("i") || choose.equals("s") || choose.equals("p") 
            || choose.equals("I") || choose.equals("S") || choose.equals("P"));

  }

 }

我不确定如何对游戏在数组中进行排序,它说要按键对游戏进行排序,但是我不知道如何在字符串中放入键.我也不擅长使用数组,所以我不知道如何将所有信息链接在一起. 多谢您的协助! 真挚地, 一个压力很大的大学生.

解决方案

就像Nimble Fungus所说,第一步是创建一个Game类,它将作为代表对象的数组.

如果您不关心效率,则Collections库具有可以使用的内置排序方法.如果您确实需要担心效率(例如,庞大的数据集),则应考虑实施更高级的排序算法.虽然,我相当确定Collections.sort实现了Merge Sort,这在大多数情况下就足够了.

要在包含对象的数据结构上使用Collections.sort,必须在方法调用中提供Comparator:

Collections.sort(array, comparator);

有关创建和使用比较器对象的信息,请查看文档. >

最后要提到的一点是,您实际上可以拥有您的Game类 implement Comparable,而不是创建一个新的比较器,该类允许您定义Game对象在创建时的自然顺序.

I am not sure how to do much with arrays yet and can't figure this out. Here is the full assignment.

  1. Write a JAVA program that maintains a list of computer games using an array. Your main program should display the following menu repeatedly.

i. insert game

s. search game

p. print list

q. Quit

Select:

The array stores a list of computer games and each game in the list consists of title(string, key), developer(string), genre(string), year of production(int), and price(float). The list should be maintained in the increasing order of the key(title).

Option i should read a game(title, developer, genre, year, price) and insert the game into the array. Note that the new game should be inserted into the right spot so that the entire array may remain sorted. Sorting entire array again after adding the new game at the end of the array is costly and hence not acceptable. Option s asks for a game title and lists all the games matching with the title entered. Note that two or more games may have the same title. Option p simply lists all the games stored in the array.

We will assume a maximum of 100 games.

And here is what I have so far

package lab12;

 import java.util.Scanner;

  // create second class to hold methods
class next {

// create "insert game" method
public void insert( ) {
    System.out.println("Insert");

}

// create "search game" method
public void search( ) {
    System.out.println("Search");

}

// create "print list" method
public void print( ) {
    System.out.println("Print");

}

// create method to 

  }

 public class Lab12 {

public static void main(String[] args) {
    Scanner in = new Scanner( System.in );

    // create string to see what user wishes to do
    String choose;

    // create instance of other class
    next choice = new next();

    do {
        // see which method user wants to use
    System.out.print("Do you want to insert game (i) or search game (s) "
            + "or print list (p) or quit (q)? ");
    // create string to see which method to go to
    choose = in.nextLine();

    // send user to correct method
    if ( choose.equals("i") || choose.equals("I"))
        choice.insert();
    else if ( choose.equals("s") || choose.equals("S") )
        choice.search();
    else if ( choose.equals("p") || choose.equals("P") )
        choice.print();

    } while ( choose.equals("i") || choose.equals("s") || choose.equals("p") 
            || choose.equals("I") || choose.equals("S") || choose.equals("P"));

  }

 }

I'm not sure how to sort the games in the array, it says to sort it by key but I don't know how to put in the key with the string. I'm not good with arrays either so I don't know how you can have all of the information linked together. Thank you for all of your help! Sincerely, A stressed out college student.

解决方案

Like Nimble Fungus stated, the first step is to create a Game class that will be your object representing games in your array.

If you're not concerned about efficiency, the Collections library had a built-in sorting method you can use. If you do need to worry about efficiency (for example, a massive data set) you should look into implementing a more advanced sorting algorithm. Although, I'm fairly certain Collections.sort implements Merge Sort, which should suffice in most situations.

To use Collections.sort on a datastructure containing objects, you must provide a Comparator in the method call:

Collections.sort(array, comparator);

For information on creating and using a comparator object, check out the documentation .

Also, if you plan on only using this comparator once, I might recommend creating an anonymous class, rather than creating a whole new class in your project. Here is the documentation for creating and using anonymous classes.

One last point to mention is that rather than creating a new comparator, you could actually have your Game class implement Comparable which would allow you to define the natural ordering of your Game objects at their creation.

这篇关于我需要使用标题,开发人员,类型等对游戏进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆