목록 객관식 질문 목록을 표시하는 프로그램을 작성하고 있습니다. 내 Java 클래스의 ChoiceString 데이터 멤버에 저장된 값에 문제가 있습니다. 클래스의 각 인스턴스에는 ChoiceString 자체의 값이 있어야하지만 어떤 이유로 ChoiceString은 첫 번째 인스턴스에 의해 초기화 된 값만 보유하고 다른 인스턴스와 공유합니다. 각 인스턴스는 고유 한 ChoiceString 고유 값을 갖기를 원합니다. 어떻게 해결할 수 있습니까? 클래스 인스턴스가 데이터 멤버의 값을 공유합니다.
다음
내 코드입니다 : 이것은 내 인스턴스에 대한 코드입니다public class AllChoiceQuestion extends ChoiceQuestion{
private String note = "Note: The following question has one or more correct choices\nYou must give the number(s) of ALL correct anwers, in order,\nto get full credit andsperated by spaces";
private int count =0;
// A varabile that will hold the user answer
private String choiceString;
public AllChoiceQuestion(String questionText) {
// initilize the question text value
super(questionText);
choiceString="";
}
public void addChoice(String choice, boolean correct){
super.addChoice(choice, correct);
if(correct == true){
count++;
choiceString += "" + count+" " ;
}
super.setAnswer(choiceString.trim());
}
public void display(){
System.out.println(note);
super.display();
}
public String toString(){
return note +"\n"+ super.toString();
}
}
ChoiceQuestion allchoicequestion1 = new AllChoiceQuestion("Which of the basic data type(s) has the size of 16 bit?");
allchoicequestion1.addChoice("Char", true);
allchoicequestion1.addChoice("Short", true);
allchoicequestion1.addChoice("Float", false);
allchoicequestion1.addChoice("Double", false);
ChoiceQuestion allchoicequestion2 = new AllChoiceQuestion("Which of the basic data type(s) has the size of 64 bit?");
allchoicequestion2.addChoice("Long", false);
allchoicequestion2.addChoice("Doulbe", false);
allchoicequestion2.addChoice("Byte", true);
allchoicequestion2.addChoice("Int", true);
allChoiceQuestion1에 대한 그래서 ChoiceStrng 1 2 해야하며 ChoiceString forallChoiceQuestion2이 있어야 할 3 4 하지만, 매번 tyee 3 4 forallChoiceQuestion2에 대한 답변으로 나에게 틀린 대답을 준다. 그러나 1 2를 입력하면 정확 할 것이다.
'AllChoiceQuestion' 클래스는 괜찮아 보입니다. 문제가 발생한 인스턴스를 생성하기위한 코드를 제공 할 수 있습니까? – Newbie
내 게시물을 편집하고 하단에 코드를 추가했습니다. – nj2012
오, 알았어. 지금 고맙다. – nj2012