谁能帮我解决这个问题,很气人。

ADAM129XU 2009-10-08 08:46:29
做完了程序,一给老师看,非要求用泛型做。结果问题就来了,上代码,大家帮忙改改谢谢
题意是这样的
是买股票的,假如说
第一次你买100股,每股20元。
第二次你买20股,每股24远。
第三次你买200股,每股36元。
现在你要卖了,卖150股,每股30元价钱卖。
你应该这么计算
100 × (30−20) + 20 × (30−24) + 30 × (30−36) = 100 × 10 + 20 × 6 + 30 × (−6)=¥940
这就是个出队列的问题,150=100+20+30,先把100出来,然后20出来,第三个出来的是200,由于还剩30要卖,所以200里只出来30个就行。
辅助程序
public class LinkedQueue {
public class Node { //节点
Node next;
int value;
Node(int val,Node n) {
value=val;
next=n;
}

}

public Node front=null; //头
public Node rear=null; //队尾

public void enqueue(int s) { //进队
if(rear!=null) {
rear.next=new Node(s,null);
rear=rear.next;
}
else {
rear=new Node(s,null);
front=rear;
}
}

public int peek() {

return front.value;
}

public int dequeue() { //出队

int value=front.value;
front=front.next;
return value;

}

public String toString() {
StringBuilder sBuilder=new StringBuilder();
Node p=front;
while(p!=null) {
sBuilder.append(p.value+" ");
p=p.next;
}
return sBuilder.toString();
}

}
测试程序
import java.util.*;
public class CapitalGain {
public static void main(String [] args) {
System.out.println("*********************Shares***********************");

String input;
char repeat;
LinkedQueue queue=new LinkedQueue();
LinkedQueue queue1=new LinkedQueue();
int buy=0;//买的个数
int sell=0;
int buymoney=0;//买的单价
int sellmoney=0;
int tempmoney=0;
int sum=0;//盈利总额
int tempyingli=0;//卖出去的个数
do{
System.out.println("Buy or sell?:");
System.out.println("enter sell or buy");
Scanner keyboard=new Scanner(System.in);
Scanner keyboard1=new Scanner(System.in);
String enter=keyboard.nextLine();//看你输入的是sell还是Buy

if(enter.equals("sell")) {
System.out.println("How many do you want to sell together:");
sell=keyboard.nextInt();
System.out.println("How much per each do you want to sell:");
sellmoney=keyboard.nextInt();

while(queue.front.value<sell) {
int temp=queue.dequeue();

sell=sell-temp;
System.out.println(queue);
tempyingli=sellmoney-queue1.front.value;
;
queue1.dequeue();

sum=sum+tempyingli*temp;
}

tempyingli=sellmoney-queue1.front.value;

sum=sum+tempyingli*sell;
queue.front.value=queue.front.value-sell;


}
else if(enter.equals("buy")) {
System.out.println("How many do you want to buy:");
buy=keyboard.nextInt();
queue.enqueue(buy);
System.out.println("How much per each do you want to buy:");
buymoney=keyboard.nextInt();
queue1.enqueue(buymoney);
}
System.out.println("Do you want to continue?");
System.out.println("enter Y to continue or enter no to check the capital gain:");
input=keyboard1.nextLine();
repeat=input.charAt(0);
}while(repeat == 'Y'|| repeat=='y');

System.out.println("The final capital gain is:"+ sum);

}

}
现在把LinkedQueue改成<T>之后,我测试程序里面那些半段的条件就出问题了,各位帮忙解决解决吧,我不行了。





















...全文
126 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigbro001 2009-10-08
  • 打赏
  • 举报
回复
最近正在研究炒股技巧,看看先。。。
ADAM129XU 2009-10-08
  • 打赏
  • 举报
回复
5楼的问题啊!!!唉呀妈呀!!谢谢啊!!我这一加个share就全搞定了。。。。。。。谢谢!!
ADAM129XU 2009-10-08
  • 打赏
  • 举报
回复
4#能加个QQ说吗?帖子有点慢
ADAM129XU 2009-10-08
  • 打赏
  • 举报
回复
4楼正解啊!!我现在改了,做了一个类型叫share,存放买的数量和价格。
public class share {
public int shareNo;//How many do you want to store;
public int shareP;//How much do you want to store for each;
public share(int No,int P) {
shareNo=No;
shareP=P;
}
public int profit(int No,int Pforeach) {
return No*Pforeach;
}
public int getshareNo() {
return shareNo;
}
public int getshareP() {
return shareP;
}

}
然后测试程序改成了
import java.util.*;
public class Test {
public static void main(String [] args) {
System.out.println("*********************Shares***********************");

String input;
char repeat;
LinkedQueue queue=new LinkedQueue();

int buy=0;//买多少个
int sell=0;//卖多少个
int buymoney=0;//每个多少钱买的
int sellmoney=0;//每个多少钱卖的
int tempmoney=0;//
int sum=0;//一共挣钱
int tempyingli=0;//每个买卖差价
int tempPrice;//用来存放每个share对象的买的时候的价钱。
do{
System.out.println("Buy or sell?:");
System.out.println("enter sell or buy");
Scanner keyboard=new Scanner(System.in);
Scanner keyboard1=new Scanner(System.in);
String enter=keyboard.nextLine();

if(enter.equals("sell")) {
System.out.println("How many do you want to sell together:");
sell=keyboard.nextInt();
System.out.println("How much per each do you want to sell:");
sellmoney=keyboard.nextInt();

while(queue.peek().getshareNo()<sell) {
int temp=queue.peek().getshareNo();
tempPrice=queue.peek().getshareP();
sell=sell-temp;

tempyingli=sellmoney-tempPrice;
;
queue.dequeue();

sum=sum+tempyingli*temp;
}
int temp=queue.peek().getshareNo();
tempPrice=queue.peek().getshareP();
tempyingli=sellmoney-tempPrice;

sum=sum+tempyingli*sell;


queue.front.value=new share(temp-sell,tempPrice);//重新设置头结点的存放的数量
}
else if(enter.equals("buy")) {
System.out.println("How many do you want to buy:");
buy=keyboard.nextInt();

System.out.println("How much per each do you want to buy:");
buymoney=keyboard.nextInt();
share s=new share(buy,buymoney);
queue.enqueue(s);
}
System.out.println("Do you want to continue?");
System.out.println("enter Y to continue or enter no to check the capital gain:");
input=keyboard1.nextLine();
repeat=input.charAt(0);
}while(repeat == 'Y'|| repeat=='y');

System.out.println("The final capital gain is:"+ sum);

}

}
这个时候要是如果LinkedList里面的value类型是share,程序就正常运转了。但是改成了泛型T之后,Test里
while(queue.peek().getshareNo()<sell)就会报错,因为找不到getshareNo()方法。后面有几步也是一样的错误
loveofmylife 2009-10-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 adam129xu 的回复:]
不好意思啊。就是整形比较的时候,泛型不让用 < 或者 >
[/Quote]
在构造的时候这样
LinkedQueue<Integer> queue=new LinkedQueue<Interger>(); 
loveofmylife 2009-10-08
  • 打赏
  • 举报
回复
晕,LZ的那个LinkedQueue里装的是什么,如果我没看错是不是放一个买的股票的数量,紧接着在放一个每股的价格??如果是这样也太乱了吧,何不把数目和价格封装在一起放进队列里。
你说的泛型,如果放的都是同一种类型,直接用LinkedQueue<T>
public class LinkedQueue<T> { 
public class Node { //节点
Node next;
T value;
Node(T val,Node n) {
value=val;
next=n;
}
}
ADAM129XU 2009-10-08
  • 打赏
  • 举报
回复
不好意思啊。就是整形比较的时候,泛型不让用 < 或者 >
csliyu 2009-10-08
  • 打赏
  • 举报
回复
你使用泛型哪里出错了 把出错的代码贴出来啊
liuxiaosabbath 2009-10-08
  • 打赏
  • 举报
回复
你不把出问题的代码贴出来,别人怎么帮你解决?

62,620

社区成员

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

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