【发布时间】: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