سوال درباره ساخت کلاس سفارشی Qustion و استفاده از آن در آرایه Cell
من میخوام یه کلاس بسازم مربوط به سوال.
یعنی توش
Private String question;
Private String answer;
و با این قابلیت که سوال چهار گزینه ای هست.
و بتونم وقتی به کاربر نشون میدم.
بتونم به صورت تصادفی سوال و جواب درست و سه گزینه غلط نشونش بدم.
میتونین راهنمایی کنین؟
نمون کد :
class Test{
String question = "";
String correctAnswer = "";
String[] otherotherOption = new String[3];
}
نمونه ساختن یک سوال :
Test class = new Test();
class.question = "q";
class.correctAnswer = "Q";
class.otherotherOption = new String[]{"A","B","C"};
نمایش سوال بصورت تصادفی :
برای نمایش گزینه ها از Math.random استفاده کنید و دقت کنید گزینه های تکراری نباشه همچنین از new Random.NextBoolean هم میتونید استفاد کنید !
Math.random.NextInt(otherotherOption.leanght());
من کدهارو همینطوری نوشتم و ممکنه مشکل syntax داشته باشه ، برای گزینه اخر هم چون نیاز به تست و اجرا شدن داره نمیتونم ذهنی راه حل رو براتون محاسبه کنم و کدش رو بزارم روش های مختلفی میشه گزینه اخر رو حل کرد ، سعی کنید یه روشی رو خودتون پیدا کنید
برخی از متد های برنامه البته اولی Constructor کلاس Cell هست
قطعه کد زیر در انتهای متد init در کلاس MainActivity درج شده است.
for (int i = 0; i < 64 - 5; i++) {
int rand = (int) (Math.random() * (63 - 5));
if (rand < 29 - 5) {
cells.add(new Cell(CellType.NORMAL));
// cell.addQuestion
} else if (rand < 55 - 5) {
cells.add(new Cell(CellType.QUESTION));
} else if (rand < 63 - 5) {
cells.add(new Cell(CellType.GIFT));
}
}
for (int i = 0; i < 64; i++) {
imgMapCells[i].setImageResource(cells.get(i).getCellImage());
}
public Cell(CellType cellType) {
this.cellType = cellType;
if (cellType == CellType.NORMAL) {
cellImage = R.drawable.ic_panorama_wide_angle_black;
} else if (cellType == CellType.QUESTION) {
Question question = new Question("2+2=?","4",{2,6,8});
questionArrayList.add(question);
cellImage = R.drawable.ic_help_outline_black;
} else if (cellType == CellType.GIFT) {
cellImage = R.drawable.ic_card_giftcard_black;
}
}
public void refreshCurrentPosition() {
for (ImageView imgArray : imgMapCells) {
imgArray.setBackgroundColor(Color.alpha(0));
}
for (Player player : players) {
imgMapCells[player.getCurrentPosition()].setBackgroundColor(player.getFlagColor());
}
}
public void movePlayer() {
final int startPosition = currentPlayer.getCurrentPosition();
final int finishPosition = startPosition + currentPlayer.getLastDice() + 1;
final Runnable handlerRunable = new Runnable() {
@Override
public void run() {
refreshCurrentPosition();
}
};
Runnable threadRunnable = new Runnable() {
@Override
public void run() {
for (int i = startPosition; i <= finishPosition; i++) {
// int j = currentPlayer.setCurrentPosition(i);
// cells[j].showDialog();
//Dialog dialog = new Dialog();
//dialog.txt_question = cell[j].getQustion.question;
//dialog.chb_1 = cell[j].getQustion.wrongAnswer[0];
//dialog.chb_2 = cell[j].getQustion.wrongAnswer[1];
//dialog.chb_3 = cell[j].getQustion.wrongAnswer[2];
//dialog.chb_4 = cell[j].getQustion.correctAnswer;
//dialog.show();
App.HANDLER.post(handlerRunable);
threadSleep(250);
}
}
};
moveThread = new Thread(threadRunnable);
moveThread.start();
}
اگر قطعه کد بالا را ببینید قطعه کد زیر را چگونه کار می کند میخواهم به همین صورت و بدون استفاده از شرط که نوع خانه را مشخص کنم و فقط یک .getQuestion بزنم
imgMapCells[i].setImageResource(cells.get(i).getCellImage());
چیزی که شبه کد زدم تا منظورمو برسونم
cells[j].showDialog();
//Dialog dialog = new Dialog();
//dialog.txt_question = cell[j].getQustion.question;
//dialog.chb_1 = cell[j].getQustion.wrongAnswer[0];
//dialog.chb_2 = cell[j].getQustion.wrongAnswer[1];
//dialog.chb_3 = cell[j].getQustion.wrongAnswer[2];
//dialog.chb_4 = cell[j].getQustion.correctAnswer;
//dialog.show();
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .