2015-09-24 21 views
0

我需要创建一个名为box的类,它使用链表来存储包含两个字符串作为输入的flashcards。链接列表Java Lietner Flashcard

public class FlashCard { 

    public static String challenge; 
    public static String response; 

    public FlashCard(String front, String back) 
    { 
     double a = Math.random(); 
     if(a > 0.5) challenge = front; 
     else challenge = back; 

     if(a < 0.5) response = front; 
     else response = back; 
    } 

    private static String getChallenge() 
    { 
     return challenge; 
    } 

    private static String getResponse(String in) 
    { 
     return response; 
    } 

    public static void main(String[] args) 
    { 
     FlashCard card = new FlashCard("Ryan Hardin", "Student at UAB"); 
     System.out.print(challenge); 
    } 

} 

这是我的盒子类使用链表作为实例变量来存储卡,但不断收到错误消息。

import java.util.LinkedList; 

public class Box { 

private LinkedList<FlashCard> data; 

public Box() { 
    this.data = new LinkedList<FlashCard>(); 

} 

public Box addCard(FlashCard r) { 
    Box one = this; 
    one.data = data.add(0,r); 

    return one; 
} 
+0

那里有问题吗?我不确定你想要什么。 – blm

+0

我不知道如何去实际添加元素到我试过的盒子,但它不起作用。我不能将元素存储在框对象中。我想知道为什么。 –

+0

您尚未在Box上提供任何方法来添加实例。如果你想对盒子做某些事情(比如添加东西,将东西排除在外,计算其中的东西等),则需要向Box类添加完成这些事情的方法。 – blm

回答

0
  • 把链表的构造为Box类的一个实例变量之外 - 让叫它boxList
  • 你的盒子的主要方法做boxList.add(新烧录卡())
+0

如果我这样做,然后不使用类,我只是添加闪卡到链接列表。 –

+0

添加一个吸气剂从Box中获取整个Flashcard列表。由于你的flashcard没有唯一的字段,你需要获取整个列表并遍历它 –