2013-12-15 3 views
0

안녕하세요 여러분, 제가 공부하는 데 도움이되는 프로그램을 만들고 있습니다. (플래시 카드 기계) 기본적으로 질문과 답변을 보유하고있는 다차원 문자열 배열을 가지고 있습니다. 처음에 질문은 무작위로 질문을 선택하는 방법이었습니다. 내가 한 것은 다른 정수 배열을 만들었고, 처음에는 그것을 섞어 두었다. 지금해야 할 일은 정수 배열을 사용하여 다차원 배열 내용을 표시하는 것입니다. 예를 들면. 내 int 배열이 {1,2,3,4}처럼 시작되었다고하자. 셔플 후 {3,1,2,4}로 바뀌었다. 이제 나는 이와 같은 정수 배열을 사용하고 싶다. ArrayOfQuestionsAndswers [IntegerArray [0]] [0] 질문을 얻으십시오. 어떻게 해야할지 모르는 것은 한 번에 한 가지 질문 만하는 것입니다. 매번 버튼을 클릭 할 때마다 정수 배열이 다음 정수로 만들어야합니다 (이 예에서는 1이됩니다). 어떻게해야합니까? 지금까지배열을 천천히 반복하는 방법

내 코드 :

Main 클래스 :

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

이 유 배열을 통해 반복하는 방법을 천천히 무엇을 의미하는지 도움이 될 것입니다 희망

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

같은 뭔가를 할 수있는 ActionListener? –

+0

당신은'이벤트 '에 의해 구동되는 것을 찾고 있습니다. 사용자가 버튼을 클릭하는 것은 '듣기'위해 필요한 '이벤트'이며, 그 후에 코드를 실행합니다. – Houseman

답변

0

수 당신은 단지 호출 된 메소드를 생성하는 것이 아닙니다. Core 클래스의 nextQuestion()은 클래스 내의 카운터를 증가시킵니다.

같은 핵심 클래스에 정수를 추가

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 클래스에서 셔플

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

그럼 당신이 말한대로 다음 decleration를 않습니다.

이제 nextbutton에서

당신이

관련 문제