Faire une erreur pour trouver une application sur Android
Les spécifications des applications présentées dans cet article sont
C'est comme ça ~~ Actuellement, c'est juste un test de vision ~~ Si vous changez la partie numérique en image, je pense que ce sera un jeu de recherche d'erreurs.
public class MainActivity extends AppCompatActivity {
private final int TOTAL_QUESTION_COUNT = 8;
TextView mTextView;
Button[] mButtons = new Button[4];
Button mNoAnsButton;
int[] mQuestions = new int[4];
HashMap<String, String> mButtonIndexs = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "Hi!", Toast.LENGTH_SHORT).show();
initViews();
storeIndexWithID();
mButtons[0].setText("PUSH");
setOnClicks();
}
private void initViews() {
mTextView = (TextView) findViewById(R.id.main_text);
mNoAnsButton = (Button) findViewById(R.id.no_answer_button);
mButtons[0] = (Button) findViewById(R.id.button1);
mButtons[1] = (Button) findViewById(R.id.button2);
mButtons[2] = (Button) findViewById(R.id.button3);
mButtons[3] = (Button) findViewById(R.id.button4);
}
private void storeIndexWithID() {
mButtonIndexs.put(String.valueOf(R.id.button1) , String.valueOf(0));
mButtonIndexs.put(String.valueOf(R.id.button2) , String.valueOf(1));
mButtonIndexs.put(String.valueOf(R.id.button3) , String.valueOf(2));
mButtonIndexs.put(String.valueOf(R.id.button4) , String.valueOf(3));
}
private void setOnClicks() {
for (int i = 0; i < mButtons.length; i++) {
final Button button = mButtons[i];
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Ans", "check:" + checkAnswer(button));
updateQuestions();
updateButtons();
}
});
}
mNoAnsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Ans", "check (NoAns):" + !existsZero(mQuestions));
updateQuestions();
updateButtons();
}
});
}
private void updateButtons() {
for (int j = 0; j < mButtons.length; j++) {
mButtons[j].setText(String.valueOf(mQuestions[j]));
}
}
private void updateQuestions() {
mQuestions = getRandomArray();
setRandomAnswerOption(mQuestions);
}
private boolean existsZero(int[] array) {
boolean exist = false;
for (int number : array) if (number == 0) exist = true;
return exist;
}
private boolean checkAnswer(Button button) {
boolean isOk = false;
if (mQuestions[Integer.parseInt(mButtonIndexs.get(String.valueOf(button.getId())))] == 0) isOk = true;
return isOk;
}
private int[] getRandomArray() {
int[] randomArray = new int[4];
for (int i = 0; i < randomArray.length; i++) {
randomArray[i] = getRandomNumber(1, TOTAL_QUESTION_COUNT);
for (int j = 0; j < i; j++) {
if (randomArray[i] == randomArray[j])
i--; // If it is duplicated, create number again
}
}
return randomArray;
}
// min ~ max - 1
private int getRandomNumber(int min, int max) {
if (max < 1) return 0;
return new Random().nextInt(max - min) + min;
}
private void setRandomAnswerOption(int[] array) {
if (getRandomNumber(0, 100) < 50) {
array[getRandomNumber(0, array.length)] = 0;
}
}
}
(Mise à jour) 07/03/2017 Le nom de la méthode et le nom de la variable ont été modifiés. Divisé certains traitements.
Recommended Posts