如何慢慢地遍历数组

时间:2013-12-15 05:28:07

标签: java arrays shuffle

嘿伙计们,我正在制作一个帮助我学习的程序。(闪存卡机)它基本上有一个多维字符串数组,可以解决问题及其答案。我遇到的第一个挑战是找到一种随机选择问题的方法。我做的是,我做了另一个整数数组,我在开始时将其洗牌。我现在需要做的是使用整数数组来显示我的多维数组内容。例如。让我们说我的int数组开始像这样{1,2,3,4}。洗牌后,它改为{3,1,2,4}。现在我想使用像这样的整数数组。 ArrayOfQuestionsAndAnswers [IntegerArray [0]] [0]得到问题。我不知道怎么做是一次得到一个问题。每次单击一个按钮时,Integer数组应该进入下一个int。(在我的例子中,这将是1)。我该怎么做?

到目前为止我的代码:

主要课程:

public class Core {

    public static void main(String[] args){
        if(Variables.getStart()){
            shuffleArray(Variables.getCardNumber());
            Variables.setStart(false);
        }
    }
   public static String getCardQuestion(){
       return Variables.getCards()[0][0];
   }
    public static String getCardAnswer(){
        return Variables.getCards()[0][1];
    }
    // Implementing Fisher–Yates shuffle
    static void shuffleArray(int[] ar)
    {
        Random rnd = new Random();
        for (int i = ar.length - 1; i > 0; i--)
        {
            int index = rnd.nextInt(i + 1);
            // Simple swap
            int a = ar[index];
            ar[index] = ar[i];
            ar[i] = a;
        }
    }

}

变量类:

 public class Variables {
        private static int[] cardNumber ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
        private static String[][] cards = {{"Apostolic Orgin", "Comes form the apostles"},
                {"Biblical inerrancy", "the doctrine that the books are free from error reading the truth"},
                {"Divine Inspiration", "the assistance the holy spirit gave the authors or the bible so they could write"},
                {"fundamentalist approach", "interpretation of the bible and christian doctrine based on the literal meaning og bible's word"}, {"pentateuch", "first 5 books of old testament"},
                {"Torah","Means law, refers to first 5 books of the old testament"},{"Sacred Scripture","The bible/approved list of Judism and Christianity"},
                {"Apostolic Succession","passing on of apostolic preaching and authority from apostles to all bishops"},
                {"encumenical council","gathering of bishops form around the world to address issues of the church"},
                {"Breviary","prayer book that contains the prayers for litergy of the hours"}};
        private static boolean start=true;
        private static int index;

        public static void setIndex(int i){
            index=i;
        }
        public static int getIndex(){
            return index;
        }
        public static void setCardNumber(int[] i){
            cardNumber=i;
        }
        public static int[] getCardNumber(){
            return cardNumber;
        }
        public static void setCards(String[][] i){
            cards=i;
        }
        public static String[][] getCards(){
            return cards;
        }
        public static void setStart(boolean i){
             start=i;
        }
        public static boolean getStart(){
            return start;

        }
    }

基本(未完成)GUI类:

import study.religion.firstfinal.core.Core;

import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GUI extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI frame = new GUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GUI() {
        setTitle("Bautista's Religion Review");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JLabel label = new JLabel("");
        label.setBounds(32, 22, 356, 160);
        contentPane.add(label);

        JButton btnShowQuestion = new JButton("Show Question");
        btnShowQuestion.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
             label.setText("Question: "+Core.getCardQuestion());
            }
        });
        btnShowQuestion.setBounds(62, 216, 121, 23);
        contentPane.add(btnShowQuestion);

        JButton btnShowAnswer = new JButton("Show Answer");
        btnShowAnswer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
             label.setText("Answer: "+Core.getCardAnswer());
            }
        });
        btnShowAnswer.setBounds(244, 216, 121, 23);
        contentPane.add(btnShowAnswer);
    }

}

2 个答案:

答案 0 :(得分:0)

你能不能在你的nextQuestion()类中创建一个名为Core的方法,该方法会增加类中的计数器?

在Core类中添加一个整数:

public class Core {

    private int counter = 0;

然后在Core类中添加一个方法:

public void nextQuestion(){
    counter++;
}

将您的getCardQuestion()getCardAnswer()修改为:

public static String getCardQuestion(){
    return Variables.getCards()[counter][0];
}

public static String getCardAnswer(){
    return Variables.getCards()[counter][1];
}

也许添加一个按钮进入下一个问题并添加一个调用nextQuestion()的ActionListener。

请注意,nextQuestion()方法将继续递增,而不检查数组边界。如果您希望nextQuestion()翻转并再次开始将语句更改为:

counter = (counter + 1) % Variables.getCards().length;

答案 1 :(得分:0)

在GUI类中执行以下删除

private int[] RandomInteger = {1,2,3,4}
private int i,j

然后如你所说,将其洗牌。

现在在nextbutton actionlistener中你可以做类似的事情

 if(i < RandomInteger.length())
{
  ArrayofQuesiontAndAnswer(randominteger[i++])[j++] //declare i and j as a class member 
}

希望它会有所帮助