开发者

how can i implement an array with an image and 3 different string values in Java

开发者 https://www.devze.com 2023-02-03 20:37 出处:网络
Good day, i will try and explain myself here. i am trying to create like a quiz system where an image is mapped to an ans开发者_JAVA百科wer and other wrong answers are provided. An example is like thi

Good day, i will try and explain myself here. i am trying to create like a quiz system where an image is mapped to an ans开发者_JAVA百科wer and other wrong answers are provided. An example is like this:

<Image> <answer A> <answer B> <answer c>

I was thinking of using a hash map for the image and correct answer, and maybe an array or an arrayList for the other wrong answers. But i don't know if hash maps works well this way.

what would be the best solution to go about this or any other ideas?

by the way, i would be inputing the values myself and the Image is stored in a resource so its not being downloaded. Thank you


Why not just define an object with an image member, and the correct/incorrect answers ?

public class ImageAnswer {
  private final Image image;
  private final String answer;
  private final String[] wrongAnswers;

  // constructor and methods follow.
}

(note sure about the name - depending on its behaviour you can rename appropriately)

That way you can define everything together (atomically) using a constructor, and embed behaviour particular to that within the object.

Remember - objects should do this for you, not provide fields for you to do things with.


What you want to do is create an object you can place all of that data in and keep it in one place.

So for example:

class Question {
    Image image;
    String correctAnswer;
    String[] wrongAnswers;
}

Then you can just play around with Question objects instead of trying to keep everything synchronized across multiple collections.


How about following:

class Answer{
String description;
Boolean correct; // true if correct and false otherwise
}

and a HashMap:

HashMap<Image,ArrayList<Answer>> quiz;


I would do it:

public class Question 
{
  private Image image;
  private String[] options;
  private int rightAnswerIndex;

  //the rest of the code; you could create a constructor that sets all the member vars
  ...

}

That way it would be much simpler to iterate through and display the options for the user. If you put the right answer separate from the other options you would always have it displayed as the first or last option.

0

精彩评论

暂无评论...
验证码 换一张
取 消