Skip to content

Instantly share code, notes, and snippets.

@aniamark
Created February 6, 2018 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniamark/1541ce3c2fa18d8b0acc9168c1e6b416 to your computer and use it in GitHub Desktop.
Save aniamark/1541ce3c2fa18d8b0acc9168c1e6b416 to your computer and use it in GitHub Desktop.
package com.example.android.quiz2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int horseScore = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void checkResults(View view) {
horseScore = 0;
// Q1, answer: a stallion, a gelding
CheckBox stallion = (CheckBox) findViewById(R.id.stallion);
boolean stallionChecked = stallion.isChecked();
CheckBox gelding = (CheckBox) findViewById(R.id.gelding);
boolean geldingChecked = gelding.isChecked();
if (stallionChecked && geldingChecked) {
horseScore = horseScore + 1;
}
// Q2, answer: chestnut
RadioButton chestnut = findViewById(R.id.chestnut);
boolean hasChestnut = chestnut.isChecked();
if (hasChestnut) {
horseScore = horseScore + 1;
}
// Q3, answer: walk
RadioButton walk = findViewById(R.id.walk);
boolean hasWalk = walk.isChecked();
if (hasWalk) {
horseScore = horseScore + 1;
}
// Q4, answer: arabian
EditText arabian = findViewById(R.id.arabian);
String checkArabian = arabian.getText().toString();
if (checkArabian.equals("arabian")) {
horseScore = horseScore + 1;
}
if (horseScore == 0) {
Toast.makeText(this, "You get: " + horseScore + " point out of 4.", Toast.LENGTH_SHORT).show();
return;
} else if (horseScore == 1) {
Toast.makeText(this, "You get: " + horseScore + " points out of 4.", Toast.LENGTH_SHORT).show();
} else if (horseScore == 2) {
Toast.makeText(this, "You get: " + horseScore + " points out of 4.", Toast.LENGTH_SHORT).show();
} else if (horseScore == 3) {
Toast.makeText(this, "You get: " + horseScore + " points out of 4.", Toast.LENGTH_SHORT).show();
} else if (horseScore == 4) {
Toast.makeText(this, "You get: " + horseScore + " points out of 4.", Toast.LENGTH_SHORT).show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment