求段代码,俱体相关功能内详。^_^

LevnWang 2008-02-25 01:16:43
1、由六个功能函数组成(Enter ,Random ,Display , Sort , Find , Quit),成功运行后出现的命令提示符为:data>
2、Quit:输入Quit,结束程序,给出结束程序的提示;
Enter:输入Enter,命令提示变为:key>,开始数据录入,每输入一个数据回车换行,当输入一个非整数时(字母或者其
它符号),数据录入过程结束。重新录入后将覆盖前面录入的数据;
Random:输入Random,例如:data>Random 100 -20 99 。在-20至99之间产生100个随机数;
Display:输入Display,显示当前数据(Enter中录入的,或者Randomk 生成的),如果还没有录入数据,输入Display
命令时,将提示先录入数据;
Sort:输入Sort,将当前数据(Enter中录入的,或者Random 生成的)升序排序;
Find:输入Find 数据,然后在数列中查找是否存在该数据,并给出提示。
3、当在命令提法符后输入各个命令时,如Quit,Q,quit,QUIT,q都有效,其它命令类似。
...全文
170 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
guozhangliang4016 2008-03-09
  • 打赏
  • 举报
回复
值得学习学习~~~~~~~
WXLTW 2008-03-04
  • 打赏
  • 举报
回复
路过学习
LevnWang 2008-02-26
  • 打赏
  • 举报
回复
根据andycpp 的代码,完成如下,新手操作,还有许多地方要大家指教。^_^
-------------------------------------------------------------------
import java.io.*;
import java.util.*;

public class Data2 {
private static ArrayList<Integer> num = new ArrayList<Integer>();

public int Menu(List<Integer> l){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
while(true) {
System.out.print("Data> ");
try {
s = br.readLine();
} catch (IOException e1) {
e1.printStackTrace();
}
if(s.trim().toLowerCase().equals("display")||s.trim().toLowerCase().equals("d")) {
Display(l);
continue;
}
if(s.trim().toLowerCase().equals("sort")||s.trim().toLowerCase().equals("s")) {
Sort(l);
continue;
}
if(s.trim().toLowerCase().startsWith("find")||s.trim().toLowerCase().startsWith("f")) {
Find(l, s);
continue;
}
if(s.trim().toLowerCase().startsWith("random")||s.trim().toLowerCase().startsWith("r")) {
Random(l, s);
continue;
}
if(s.trim().toLowerCase().equals("enter")||s.trim().toLowerCase().equals("e"))
return 2;
if(s.trim().toLowerCase().equals("quit")||s.trim().toLowerCase().equals("q"))
return 3;
System.out.println("error command!");
}
}

//Enter function
public int Enter(List<Integer> l){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
l.clear();
while(true) {
System.out.print("Key> ");
try {
s = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
l.add(Integer.valueOf(s.trim()));
}catch(NumberFormatException e){
return 1;
}
}
}

//display function
private void Display(List<Integer> l){
if(l.isEmpty()) {
System.out.println("no data!");
}else {
for(Integer i:l) {
System.out.print(i+",");
}
System.out.println();
}
}
//sort function
private void Sort(List<Integer> l) {
if(l.isEmpty()) {
System.out.println("no data");
}else {
System.out.println("sort finished!");
Collections.sort(l);
}
}
//find function
private void Find(List<Integer> l, String s) {
String[] ss = s.split("\\s");
if(ss[0].toLowerCase().equals("find")||ss[0].toLowerCase().equals("f")) {
int i = 0;
try {
i = Integer.parseInt(ss[1]);
}catch(NumberFormatException e) {
System.out.println("error command!");
return;
}

if(l.isEmpty()) {
System.out.println("no data!");
}else {
int index = l.indexOf(new Integer(i));
if(index == -1)
System.out.println("no found the number "+i);
else
System.out.println("number "+i+" is the "+index+"th");
}
}
else{
System.out.println("error command!");
return;
}
}
//Random function
public void Random(List<Integer> l,String s){
String[] ss = s.split("\\s");
if (ss[0].toLowerCase().equals("Random")||ss[0].toLowerCase().equals("r")){
int N = 0;
int Max = 0;
int Min = 0;
try{
N = Integer.parseInt(ss[1]);
Min = Integer.parseInt(ss[2]);
Max = Integer.parseInt(ss[3]);
}catch(NumberFormatException e){
System.out.println("Error command!");
return;
}

for(int i=0;i<N;i++){
try{
l.add((int)((Max-Min)*Math.random()+Min));
}catch(NumberFormatException e){
System.out.println("Error command!");
}
}
}
else{
System.out.println("error command!");
return;
}
}
//main function
public static void main(String[] args) {
// TODO Auto-generated method stub
Data2 data = new Data2();
int i = 1;
while(true) {
switch(i){
case 1:
i = data.Menu(num);
break;
case 2:
i = data.Enter(num);
break;
case 3:
System.out.println("thanks ,bye bye!");
return;
}
}
}
}
zdjray 2008-02-25
  • 打赏
  • 举报
回复
看来老师对C情有独钟~
andycpp 2008-02-25
  • 打赏
  • 举报
回复
简单的做了一个,与要求不完全一致,差别如下:
1、Key提示符用于输入命令,Data提示符用于输入数据,我个人感觉这样更好一些。
2、不支持命令缩写
3、不支持Random命令

2、3两条可供楼主自己练习提高,呵呵~~~~~~~~

下面给出代码,所有类均打包在andycpp包中。以下代码在Eclipse3.3中调试通过。
1、主类,程序的入口。

package andycpp;

import java.util.ArrayList;

public class MyShell {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyShell shell = new MyShell();
shell.run();
}

public void run() {
Data d = new Data();
Key k = new Key();
int i = 1;
while(true) {
switch(i) {
case 1:
i = k.run(num);
break;
case 2:
i = d.run(num);
break;
case 3:
System.out.println("欢迎使用本系统,再见!!");
return;
}
}
}

private ArrayList<Integer> num = new ArrayList<Integer>();
}


2、Key类,用于处理key提示符

package andycpp;

import java.io.*;
import java.util.Collections;
import java.util.List;

public class Key {
public int run(List<Integer> l){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
while(true) {
System.out.print("Key> ");
try {
s = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(s.trim().toLowerCase().equals("display")) {
doDisplay(l);
continue;
}
if(s.trim().toLowerCase().equals("sort")) {
doSort(l);
continue;
}
if(s.trim().toLowerCase().startsWith("find")) {
doFind(l, s);
continue;
}
if(s.trim().toLowerCase().equals("enter"))
return 2;
if(s.trim().toLowerCase().equals("quit"))
return 3;
System.out.println("错误的命令!!!");
}
}

//处理命令display
private void doDisplay(List<Integer> l){
if(l.isEmpty()) {
System.out.println("目前尚未存储任何数据!");
}else {
for(Integer i:l) {
System.out.print(i+" ");
}
System.out.println();
}
}

//处理sort命令
private void doSort(List<Integer> l) {
if(l.isEmpty()) {
System.out.println("目前尚未存储任何数据,无法排序!");
}else {
System.out.println("所有数据已经按照升序排列!!!");
Collections.sort(l);
}
}

//处理find命令
private void doFind(List<Integer> l, String s) {
String[] ss = s.split("\\s");
if(!ss[0].equals("find")) {
System.out.println("错误的命令!!!");
return;
}
int i = 0;
try {
i = Integer.parseInt(ss[1]);
}catch(NumberFormatException e) {
System.out.println("错误的命令!!!");
return;
}


if(l.isEmpty()) {
System.out.println("目前尚未存储任何数据,无法查找!");
}else {
int index = l.indexOf(new Integer(i));
if(index == -1)
System.out.println("未找到数字 "+i);
else
System.out.println("数字 "+i+" 位于第"+index+"位");
}
}
}


3、Data类,用于处理Data提示符

package andycpp;

import java.io.*;
import java.util.List;

public class Data {

public int run(List<Integer> l){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
int i;
l.clear();
while(true) {
System.out.print("Data> ");
try {
s = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
l.add(Integer.valueOf(s.trim()));
}catch(NumberFormatException e){
return 1;
}
}
}
}
  • 打赏
  • 举报
回复
学习了...
dgqbcht 2008-02-25
  • 打赏
  • 举报
回复
解决这样的问题需要付费。
诗海 2008-02-25
  • 打赏
  • 举报
回复
不是拿分狂都不应该回答lz这种无聊的、浪费他人时间的、极度偷懒的问题。
zs_han 2008-02-25
  • 打赏
  • 举报
回复
帮顶

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧