做完了程序,一给老师看,非要求用泛型做。结果问题就来了,上代码,大家帮忙改改谢谢
题意是这样的
是买股票的,假如说
第一次你买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>之后,我测试程序里面那些半段的条件就出问题了,各位帮忙解决解决吧,我不行了。
题意是这样的
是买股票的,假如说
第一次你买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>之后,我测试程序里面那些半段的条件就出问题了,各位帮忙解决解决吧,我不行了。
你说的泛型,如果放的都是同一种类型,直接用LinkedQueue<T>
public class LinkedQueue<T> {
public class Node { //节点
Node next;
T value;
Node(T val,Node n) {
value=val;
next=n;
}
}
LinkedQueue<Integer> queue=new LinkedQueue<Interger>();
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()方法。后面有几步也是一样的错误