2013-12-15 44 views
0

嗨,我正在製作一個幫助我學習的程序(一個閃存卡機器)它基本上有一個多維字符串數組來保存問題和答案。我過來的第一個挑戰是找到一種隨機挑選問題的方法。我做的是,我做了另一個整數數組,我開始洗牌。我現在需要做的是使用整數數組來顯示我的多維數組內容。例如。可以說我的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); 
    } 

} 
+0

什麼ü意味着如何慢慢從一個數組迭代? –

+0

您正在尋找由'events'驅動的東西。用戶點擊一個按鈕是一個'事件',你需要'聽',然後執行代碼。 – Houseman

回答

0

燦你不只是創建一個叫做的方法nextQuestion()在你的Core類中增加班級內的計數器?

一個整數的核心類添加爲這樣:

public class Core { 

    private int counter = 0; 

然後在你的核心類添加一個方法,例如:

public void nextQuestion(){ 
    counter++; 
} 

修改您的getCardQuestion()getCardAnswer()到:

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

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

也許增加一個進入下一個問題的按鈕並添加一個ActionL這叫做nextQuestion()

請注意,nextQuestion()方法將保持遞增而不檢查數組邊界。如果你想nextQuestion()翻身,並開始再次更改聲明這樣的:

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

在你的GUI類做以下decleration

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

然後就像你說的,將它洗。

現在Next按鈕的ActionListener,你可以做這樣的事情

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

希望這將有助於