【问题标题】:Emulate a java sorting shell on the eclipse command prompt在 eclipse 命令提示符下模拟 java 排序 shell
【发布时间】:2015-10-21 05:19:19
【问题描述】:

我已经制作了一个程序,我正在尝试使用 Eclipse 制作一个“排序外壳”。我可以做的一些基本功能是加载要使用的排序算法,卸载它,使用算法排序并使用特定 ID 从 shell 卸载算法。

我为所有排序算法(Bubble、Insertion、Quick 等)设置了单独的类,但我不知道如何将它们加载到 shell 中。这是我目前所拥有的:

public static void main(String[] args) {
    boolean exit_loop = true;

    while (exit_loop){

        String[] command;
        command = input();
        switch(command[0]){
            case "load":
                // ???
                }
                break;
            case "tag":
                break;
            case "sort":
                break;
            case "unload":
                break;
            case "quit":
                exit_loop = false;
                break;
            default: 
                System.out.println("Input a valid command!");
                break;
        }
    }
}

public static String[] input(){
    Scanner user_input = new Scanner( System.in );
    String command;
    System.out.print("sort:>");
    command = user_input.next();
    String[] first = command.split("\\s+");
    return first;
}

因此,即使我能够获得用户输入,我也不确定如何实际加载我的排序算法类(我已将其实现为单独的类)。

以下是我如何实现一种排序算法的示例:

class Bubble implements SortingAlgorithms{

public int[] sort(int[] array){

    int temp;
    boolean swap = true;

    while(swap){
        swap = false;
        for (int i=0; i < array.length-1; i++){

            if (array[i] > array[i + 1]){
                temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = temp;
                swap = true;
            }
        }
    }
    return array;
}

private String id;
public String name() {
    return id;
}

public void name(String name) {
    id = name;
}

任何帮助将不胜感激!

【问题讨论】:

  • “负载”应该做什么?
  • 将排序算法加载到 shell 中。例如,如果我的类名为“Bubble”,“sort:>load Bubble”应该在环境中初始化该类。
  • 这个初始化在做什么?您的代码只是对某些东西进行排序。所要做的就是调用排序类的 new 并调用 sort 方法。只需在用户输入“排序”时实现这两行即可。
  • 由于有 5 种排序算法(我只发布了 Bubble 以防止帖子变得太长),如果我加载 Bubble,那么每次我们想要对某些内容进行排序时,我都会使用 Bubble 进行排序只有,直到卸载。这就是我想要完成的。我也试试你的方法!谢谢!
  • 首先,您的问题实际上似乎与Eclipse 无关。其次,您没有向我们展示您尝试过的内容以及失败的原因=>因此您有点错过了您想要在这里实现的目标。如果你确实只有 5 个固定类,简单的switch 语句就可以了。如果您坚持在运行时加载外部类,这也是可能的(简单的例子是stackoverflow.com/questions/9886266/…),但是您的问题缺少很多为什么这些方法不适合它的细节。

标签: java eclipse algorithm shell sorting


【解决方案1】:

我假设你知道如何编写代码来创建和填充数组,以及如何打印结果,所以我不会在这里费心。

public static void main(String[] args) {
  boolean exit_loop = false;
  SortingAlgorithms selectedalgo = null;
  int[] array = null;

  while (!exit_loop) {
    String[] command;
    command = input();

    switch (command[0]) {
      case "load":
        SortingAlgorithms newalgo = null;
        String algoname = command[1]; // "Bubble" etc.
        try {
          Class c = Class.forName(algoname);
          newalgo = c.newInstance();
        }
        catch (Exception e) {
        }
        if (newalgo == null) {
          System.out.println("Unknown algorithm: " + algoname);
        }
        else {
          selectedalgo = newalgo;
        }
        break;

      case "sort":
        if (selectedalgo == null) {
          System.out.println("Load an algorithm first!");
        }
        else if (array == null) {
          System.out.println("Provide an array first!");
        }
        else {
          array = selectedalgo.sort(array);
        }
        break;

      case "unload":
        selectedalgo = null;
        break;

      case "quit":
        exit_loop = true;
        break;

      default: 
        System.out.println("Input a valid command!");
        break;
    }
  }
}

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 2012-08-11
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多