这么快就课程设计了?? 呵呵 看到我以前交作业的也苦苦求代码上弄了近1个小时终于弄出来了 我简单调试了你举的那个例子 没问题 其他的你自己调试吧
import java.io.*;
import java.util.*;
public class QueueTest {
public int value;
public int num;
public QueueTest(int value, int num) {
this.value = value;
this.num = num;
}
public static void main(String args[]) {
Queue list = new LinkedList();
InputStreamReader isr = null;
BufferedReader br = null; //从命令行读取数据用的
try {
while(true) {
System.out.println("1 买股票");
System.out.println("2 卖股票");
System.out.println("3 退出");
isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
int i = -1;
int value = 0;
int num = 0;
try {
i = Integer.parseInt(br.readLine()); //读取命令操作代号
} catch(NumberFormatException ne) {
System.out.println("输入错误");
}
if(i == 1) {
System.out.println("请输入股价");
isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
try {
value = Integer.parseInt(br.readLine());
} catch(NumberFormatException ne) {
System.out.println("输入错误");
continue; //读取所输入股价 股票数目
}
System.out.println("请输入买入数目");
isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
try {
num = Integer.parseInt(br.readLine());
} catch(NumberFormatException ne) {
System.out.println("输入错误");
continue;
}
list.add(new QueueTest(value, num)); //向队列中加入购股记录
} else if(i == 2) {
int income = 0;
System.out.println("请输入股价");
isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
try {
value = Integer.parseInt(br.readLine());
} catch(NumberFormatException ne) {
System.out.println("输入错误");
continue;
}
System.out.println("请输入卖出数目");
isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
try {
num = Integer.parseInt(br.readLine());
} catch(NumberFormatException ne) {
System.out.println("输入错误"); //与上同
continue;
}
while (list.peek() != null) {
QueueTest t = (QueueTest)list.peek();
if(t.num > num) {
t.num -= num;
income += num * (value - t.value);
System.out.println("收益是:" + income);
break; //当购股记录股数大于卖出剩余量时,添加收益,退出队列访问
} else {
num -= t.num;
income += t.num * (value - t.value);
list.poll(); //当购股记录股数大于卖出剩余量时,添加收益,继续访问
}
}
if (list.peek() == null) { //现存股总数小于卖出数,将存货全卖出,输出还需要但没卖出的股数以及收益
System.out.println("已将剩余全部股票卖出,还缺少卖出股票数目:" + num);
System.out.println("收益是:" + income);
}
} else if(i == 3) {
return;
} else {
System.out.println("输入错误");
}
}
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null) br.close();
if(isr != null) isr.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
} |