gpt4 book ai didi

java - 我已经为数组编写了代码,但 logcat 没有显示错误

转载 作者:行者123 更新时间:2023-12-01 18:27:27 25 4
gpt4 key购买 nike

我遇到了一个问题,但它没有显示在 LOGCAT 中,因为它不是代码错误或 null问题是,如果问题有2 个答案,并且用户选择了这两个答案,则算作正确答案,但如果问题只有 1 个答案,则游戏会将其计算为错误答案即使用户选择了正确的答案,我也会发布该答案的屏幕截图 enter image description here我已经编码,正确答案将是粗体和红色,如您所见

这是我的问题 fragment 代码


public class QuestionFragment extends Fragment implements IQuestion {

TextView txt_question_text;
CheckBox ckbA, ckbB, ckbC, ckbD;
FrameLayout layout_image;
ProgressBar progressBar;

Question question;
int questionIndex = -1;


public QuestionFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View itemView = inflater.inflate(R.layout.fragment_question, container, false);
//GET QUESTION
questionIndex = getArguments().getInt("index", -1);
question = Common.questionList.get(questionIndex);

if (question != null) {
layout_image = (FrameLayout) itemView.findViewById(R.id.layout_image);
progressBar = (ProgressBar) itemView.findViewById(R.id.progress_bar);
if (question.isImageQuestion()) {
ImageView img_question = (ImageView) itemView.findViewById(R.id.img_question);
Picasso.get().load(question.getQuestionImage()).into(img_question, new Callback() {
@Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}

@Override
public void onError(Exception e) {
Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();

}
});
} else
layout_image.setVisibility(View.GONE);

//VIEW
txt_question_text = (TextView) itemView.findViewById(R.id.txt_question_text);
txt_question_text.setText(question.getQuestionText());

ckbA = (CheckBox) itemView.findViewById(R.id.ckbA);
ckbA.setText(question.getAnswerA());
ckbA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbA.getText().toString());
else
Common.selected_values.remove(ckbA.getText().toString());
}
});

ckbB = (CheckBox) itemView.findViewById(R.id.ckbB);
ckbB.setText(question.getAnswerB());
ckbB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbB.getText().toString());
else
Common.selected_values.remove(ckbB.getText().toString());
}
});

ckbC = (CheckBox) itemView.findViewById(R.id.ckbC);
ckbC.setText(question.getAnswerC());
ckbC.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbC.getText().toString());
else
Common.selected_values.remove(ckbC.getText().toString());
}
});

ckbD = (CheckBox) itemView.findViewById(R.id.ckbD);
ckbD.setText(question.getAnswerD());
ckbD.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Common.selected_values.add(ckbD.getText().toString());
else
Common.selected_values.remove(ckbD.getText().toString());
}
});


}
return itemView;
}

@Override
public CurrentQuestion getSelectedAnswer() {
//THIS METHOD WILL RTURN STATE OF ANSWER
//RIGHT , WRONG OR NO ANSWER
CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
StringBuilder result = new StringBuilder();
if (Common.selected_values.size() > 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
for (int i = 0; i < arrayAnswer.length; i++)
if (i < arrayAnswer.length - 1)
result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
else
result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
} else if (Common.selected_values.size() == 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
result.append((String) arrayAnswer[0]).substring(0, 1);
}

if (question != null) {
//COMPARE CORRECT ANSWER FROM DATABASE WITH USER SELECTED ANSWER
if (!TextUtils.isEmpty(result)) {
if (result.toString().equals(question.getCorrectAnswer()))
currentQuestion.setType(Common.ANSWER_TYPE.RIGHT_ANSWER);
else
currentQuestion.setType(Common.ANSWER_TYPE.WRONG_ANSWER);
} else
currentQuestion.setType(Common.ANSWER_TYPE.NO_ANSWER);
}
else {
Toast.makeText(getContext(), "cannot get question", Toast.LENGTH_SHORT).show();
currentQuestion.setType(Common.ANSWER_TYPE.NO_ANSWER);
}
Common.selected_values.clear();//ALWAYS CLEAR SELECTED ANSWERS AFTER COMPARE IS DONE

return currentQuestion;
}

@Override
public void showCorrectAnswer() {

//BOLD CORRECT ANSWER
//PARTERN:A,B
String[] correctAnswer = question.getCorrectAnswer().split(",");
for (String answer : correctAnswer) {
if (answer.equals("A")) {
ckbA.setTypeface(null, Typeface.BOLD);
ckbA.setTextColor(Color.RED);
} else if (answer.equals("B")) {
ckbB.setTypeface(null, Typeface.BOLD);
ckbB.setTextColor(Color.RED);
} else if (answer.equals("C")) {
ckbC.setTypeface(null, Typeface.BOLD);
ckbC.setTextColor(Color.RED);
} else if (answer.equals("D")) {
ckbD.setTypeface(null, Typeface.BOLD);
ckbD.setTextColor(Color.RED);
}
}

}

@Override
public void disapleAnswer() {

ckbA.setEnabled(false);
ckbB.setEnabled(false);
ckbC.setEnabled(false);
ckbD.setEnabled(false);


}

@Override
public void resetQuestion() {

//ENABLE CHECKBOX
ckbA.setEnabled(true);
ckbB.setEnabled(true);
ckbC.setEnabled(true);
ckbD.setEnabled(true);

//REMOVE ALL SELECTED
ckbA.setChecked(false);
ckbB.setChecked(false);
ckbC.setChecked(false);
ckbD.setChecked(false);

//REMOVE ALL BOLD ON TEXT
ckbA.setTypeface(null, Typeface.NORMAL);
ckbA.setTextColor(Color.BLACK);

ckbB.setTypeface(null, Typeface.NORMAL);
ckbB.setTextColor(Color.BLACK);

ckbC.setTypeface(null, Typeface.NORMAL);
ckbC.setTextColor(Color.BLACK);

ckbD.setTypeface(null, Typeface.NORMAL);
ckbD.setTextColor(Color.BLACK);

}
}

我认为错误将出现在逻辑上的问题 fragment 的这部分中,但无法获取它(这是因为正确答案是否有2个值或1个值)

 @Override
public CurrentQuestion getSelectedAnswer() {
//THIS METHOD WILL RTURN STATE OF ANSWER
//RIGHT , WRONG OR NO ANSWER
CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
StringBuilder result = new StringBuilder();
if (Common.selected_values.size() > 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
for (int i = 0; i < arrayAnswer.length; i++)
if (i < arrayAnswer.length - 1)
result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
else
result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
} else if (Common.selected_values.size() == 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
result.append((String) arrayAnswer[0]).substring(0, 1);
}

最佳答案

刚刚找到答案,但我不会删除该帖子,因此如果其他人遇到同样的问题

我只是删除了第二部分并编辑了第一部分条件,如下

 CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
StringBuilder result = new StringBuilder();
if (Common.selected_values.size() >= 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
for (int i = 0; i < arrayAnswer.length; i++)
if (i < arrayAnswer.length - 1)
result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
else
result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
} /*else if (Common.selected_values.size() == 1) {
Object[] arrayAnswer = Common.selected_values.toArray();
result.append((String) arrayAnswer[0]).substring(0, 1);
}*/

关于java - 我已经为数组编写了代码,但 logcat 没有显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60209995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com