简体   繁体   中英

Java 2D array storing many different types of data

This is my first Stack question.. so any help is appreciated(about posting style or given problem)

So.. I'm not really "new" in Java, but there are things that I don't know about it.

I have a question about how to store following: "Which of these is a fruit", "Orange", "Brick", "Tiger", "(and some kind of indication of right answer)".

Bear in mind that there would be no more than 20 of theses kind of records, so i would put them in by hand statically not dynamically

I was thinking about using a 2d array, but how and will it work. My tutor also said that 2d arrays would be the best way to go ( i think he is expecting a 2d array even if there is a better way)... He also said that it MUST be as simple as possible so any fancy ways of Java must be hold off.

I could manage the 2d array part.. but how do i indicate the right answer? Some kind of Boolean with true or false?

The part about collecting answers from the user and checking is not that big of problem for me.

I would like your suggestions on how to deal with this task. Thank you!

ps it MUST be something that applet could handle EDIT: ok guys .. i went whit 2d array here is code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;

    public class Quiz_applet extends JApplet implements ActionListener {
    static int rez = 0;
    static int c=0;
    static JTextField input = new JTextField(3);
    static String[][] data = {//q1 is in field data[0][0]
    {"q1", "A1", "A2", "A3", "A"},
    {"q2", "A1", "A2", "A3", "B"},
    {"q3", "A1", "A2", "A3", "C"},
    {"q4", "A1", "A2", "A3", "B"},
    {"q5", "A1", "A2", "A3", "C"},
    {"q6", "A1", "A2", "A3", "A"},
    {"q7", "A1", "A2", "A3", "A"},
    {"q8", "A1", "A2", "A3", "B"},
    {"q9", "A1", "A2", "A3", "C"},
    {"q10", "A1", "A2", "A3", "A"},
    {"q11", "A1", "A2", "A3", "A"}

    };
public void init() {     
    ShapeCanvas canvas = new ShapeCanvas();      
    JButton Button = new JButton("Answer!");
    Button.addActionListener(canvas);
    input.addActionListener(canvas);        
    JPanel bottom = new JPanel(); 
    bottom.setLayout(new GridLayout(1,4,3,3));     
    bottom.add(Button);
    bottom.add(input);
    getContentPane().setLayout(new BorderLayout(3,3));
    getContentPane().add("Center",canvas);              
    getContentPane().add("South",bottom);
    setSize(600,300);
}
static class ShapeCanvas extends JPanel
implements ActionListener {
    ShapeCanvas() {

}   
    public void paintComponent(Graphics g) {

if(c==0){
    g.drawString(data[0][0], 50, 100);
    g.drawString(data[0][1], 50, 120);
    g.drawString(data[0][2], 50, 140);
    g.drawString(data[0][3], 50, 160);
}
else if(c!=0){
     g.drawString(data[c][0], 50, 100);
     g.drawString(data[c][1], 50, 120);
     g.drawString(data[c][2], 50, 140);
     g.drawString(data[c][3], 50, 160);
     //repaint();
}


}

    public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}
    }   

@Override
public void actionPerformed(ActionEvent evt) {
     String command = evt.getActionCommand();



    if (command.equals("Answer!"))
     {
         if(input.getText().equals(data[c][4]))
     {
     c=+1;
     rez=+1;
     };
         } ;


}
}

How to make question to change after i have clicked the answer button? On other projects of mine it has worked something like this^. ps2. I know the grid layout is bit "weird" but it works.. :)

Well there are certainly better ways of doing this such as using a Map which would allow you to map "Orange" -> true/false

However, if it must be a 2d array you can do the following:

private final String YES = "Y";
private final String NO = "N";

String[20][2] fruitData = new String[20][2]();

fruitData[0][0] = "Orange";
fruitData[0][1] = YES;

fruitData[1][0] = "Brick";
fruitData[1][1] = NO;

This is the sort of thing that makes sense as an object. That allows you to pair the question to answers in a way that makes sense. Its not a 2D array but theres nothing overly fancy here. You can then store these Question objects in a 1D array

public class Question {
    String question;
    List<String> allAnswers=new ArrayList<>();
    String correctAnswer;

    public Question(String question,String correctAnswer, List<String> wrongAnswers) {
        this.question = question;
        this.correctAnswer = correctAnswer;

        allAnswers.addAll( wrongAnswers);

        //add the correct answer somewhere at random (so that when printing the options it isn't always the first one)
        Random rnd=new Random();
        allAnswers.add( rnd.nextInt(wrongAnswers.size()), correctAnswer);

    }

    public boolean isCorrectAnswer(String answer){
        return correctAnswer.equalsIgnoreCase(answer);
    }

    @Override
    public String toString() {
        //string builder would be better but may constitute "fancy"
        String questionAsString= question;

        for(String answer: allAnswers){
            questionAsString=questionAsString+"\n";
            questionAsString=questionAsString+answer;
        }

        return questionAsString;
    }


    public static void main(String[] args){
        List<String> incorrectAnswers=new ArrayList<>();
        incorrectAnswers.add("Brick");
        incorrectAnswers.add("Tiger");

        Question question=new Question("Which of these is a fruit","Orange", incorrectAnswers);

        System.out.println(question);

        System.out.println(); //just putting in a blank line

        System.out.println(question.isCorrectAnswer("Brick"));
        System.out.println(question.isCorrectAnswer("orange"));

    }


}

In a 2D array, you could make the last column the index of the correct answer. Then you would convert the index to an int and retrieve the correct answer:

Format:

question, choice1, choice2, choice3, correct index

Sample records:

"Which of these is a fruit", "Orange", "Brick", "Tiger", "1"

"Which of these is a car", "Turtle", "Volkswagen", "Mouse", "2"

In the first record, "1" is the array index for "Orange" (the correct answer). In the second record, "2" is the array index for "Volkswagen".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM