2016-03-31 22 views
-4

Bu programın amacı, bağlantılı bir listeyi kullanarak bir kart destesi tasarlamaktır. Kodlarım, çalışırken herhangi bir şey yazdırmıyor. İşteBağlı bir listeyi kullanarak bir kart destesi tasarımı

Düğüm Class aşağıda kodum:

public class Node 
{ 
private Comparable data; 
private Node next; 
public Node() 
{ 
next = null; 
} 
public Node(Comparable c) 
{ 
data = c; 
next = null; 
} 
public Node(Comparable c, Node n) 
{ 
data = c; 
next = n; 
} 
public Comparable getData() 
{ 
return data; 
} 
public void setData(Comparable c) 
{ 
data = c; 
} 
public Node getNext() 
{ 
return next; 
} 
public void setNext(Node n) 
{ 
next = n; 
} 

} 

LinkedList sınıfı:

public class LinkedList 
{ 
private Node first = null; 
private Node current = null; 
private Node pre = null; 
public boolean isEmpty() 
{ 
return true; 
} 
public boolean contains(Comparable item) 
{ 
current = first; 
pre = null; 
while ((current != null)&&(current.getData().compareTo(item) < 0)) 
{ 
pre = current; 
current = current.getNext(); 
} 
return ((current != null) && (current.getData().compareTo(item) == 0)); 
} 
public int size() 
{ 
int count = 0; 
current = first; 
pre = null; 
while (current != null) 
{ 
pre = current; 
current = current.getNext(); 
count++; 
} 
return count; 

} 
public void add(Comparable c) 
{ 

Node temp = new Node(c); 
if (pre == null) 
{ 
first = temp; 
} 
else 
{ 
pre.setNext(temp); 
} 
temp.setNext(current); 
current = temp; 
} 
public void remove(Comparable c) 
{ 
if (pre == null) 
{ 
first = first.getNext(); 
} 
else 
{ 
current = current.getNext(); 
if (pre == null) 
{ 
first = current; 
} 
else 
{ 
pre.setNext(current); 
} 

} 

} 
public void clear() 
{ 
first = null; 
} 
public void print() 
{ 
Node current = first; 
while (current != null) 
{ 
System.out.println(current.getData()); 
current = current.getNext(); 
} 
} 
} 

Kart sınıfı:

public class Card implements Comparable<Card> 
{ 
private int rank; 
private int suit; 
public Card(int suit, int rank) 
{ 
this.rank = rank; 
this.suit = suit; 
} 
public int getRank() 
{ 
return rank; 
} 
public int getSuit() 
{ 
return suit; 
} 
public String toString() 
{ 
switch(suit) 
{ 
case 1: 
switch(rank) 
{ 
case 11: return "Jack of Hearts"; 
case 12: return "Queen of Hearts"; 
case 13: return "King of Hearts"; 
case 14: return "Ace of Hearts"; 
default: return rank + " of Hearts"; 
} 

case 2: 
switch(rank) 
{ 
case 11: return "Jack of Diamonds"; 
case 12: return "Queen of Diamonds"; 
case 13: return "King of Diamonds"; 
case 14: return "Ace of Diamonds"; 
default: return rank + " of Diamonds"; 
} 
case 3: 
switch(rank) 
{ 
case 11: return "Jack of Clubs"; 
case 12: return "Queen of Clubs"; 
case 13: return "King of Clubs"; 
case 14: return "Ace of Clubs"; 
default: return rank + " of Clubs"; 
} 
case 4: 
switch(rank) 
{ 
case 11: return "Jack of Spades"; 
case 12: return "Queen of Spades"; 
case 13: return "King of Spades"; 
case 14: return "Ace of Spades"; 
default: return rank + " of Spades"; 
} 
} 
return null; 
} 
public int compareTo(Card a) 
{ 
if (this.rank < a.rank) 
{ 
return -1; 
} 
if (this.rank > a.rank) 
{ 
return 1; 
} 
if (this.rank == a.rank) 
{ 
if (this.suit < a.suit) 
{ 
return -1; 
} 
if (this.suit > a.suit) 
{ 
return 1; 
} 
} 

return 0; 
} 
} 

CardDeck sınıfı - kartın bir bağlantılı liste:

import java.util.Random; 

public class CardDeck 
{ 
private LinkedList cards; 
private int numCards; 
public void Deck() 
{ 
for (int a = 1; a <= 4; a++) 
{ 
for (int b = 1; b <= 14; b++) 
{ 
cards.add(new Card(a,B)/>); 
} 
} 
} 

public void drawFromDeck() 
{ 
Random rand = new Random(); 
int index = rand.nextInt(cards.size()); 
cards.remove(index); 
numCards--; 
} 
public int getTotalCard() 
{ 
return cards.size(); 
} 
} 

Ana sınıfı: Mevcut kodunda

public class Main 
{ 
public static void main(String[] args) 
{ 
LinkedList myList = new LinkedList(); 
CardDeck myCards = new CardDeck(); 
myCards.Deck(); 
myList.print(); 
} 
+0

Kodunuzda herhangi bir girinti yok, bunların tümünü haklı çıkartacak ve okumak, anlamak ve hata ayıklamak neredeyse imkansız hale getiriyor. Lütfen posta kodunuzu, genellikle blok başına 4 boşluk olacak şekilde uygun girintiler vererek ve aynı bloktaki tüm kodların aynı girinti seviyesinde olduğundan emin olarak yeniden biçimlendirin. Bu konudaki işbirliğiniz büyük ölçüde takdir edilecek ve muhtemelen iyi ve hızlı bir yanıt alma şansınızı artıracaktır. –

+0

biçimlendirmenizi düzeltin lütfen –

+0

Bir Bağlantısız myList değişkeni oluşturmak için ana içinde hiçbir şey yapmaz ve hiçbir amaca hizmet etmez - neden? Amaç olmadan kod oluşturmayın, ve böylece bu boşa değişkeni kurtulmak ve hiçbir şey tutmaz olarak üzerine baskı aramak için çalışmayın. Bunun yerine myCards değişkeninizin içeriğini yazdırmanız gerekir. –

cevap

0

, myList, sen çıktısını alıyor, yapıcı çağrıldığını geçmiş modifiye asla. myCard'un içeriğini yazdırmak isterseniz, yazdırma deyimlerini bu sınıftaki bir yöntemde koymanız ve bu yöntemi çağırmak için güverte içeriğini yazdırmak veya cards için bir alıcı oluşturmak ve onu sınıfın dışına yazdırmak gerekir.

0

Hatalı program. cards.add (yeni Kart (a, B) />); Bu satır, numaralı bir özel durumu başlatmamalısınız. Onun null.

İlgili konular