Untitled
unknown
plain_text
a year ago
2.4 kB
1
Indexable
Never
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import java.util.Random; public class MainActivity extends AppCompatActivity { int result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Random rand1 = new Random(); int randomNum1 = rand1.nextInt((9 - 1) + 1) + 1; Random rand2 = new Random(); int randomNum2 = rand2.nextInt((9 - 1) + 1) + 1; String operationrandom; Random rand3 = new Random(); int operationrandomnumber = rand3.nextInt((4 - 1) + 1) + 1; switch (operationrandomnumber){ case 1: operationrandom = "+"; result = randomNum1 + randomNum2; break; case 2: operationrandom = "-"; result = randomNum1 - randomNum2; break; case 3: operationrandom = "*"; result = randomNum1 * randomNum2; break; case 4: operationrandom = "/"; result = randomNum1 / randomNum2; break; default: operationrandom = "+"; result = randomNum1 + randomNum2; break; } TextView numara1 = (TextView) findViewById(R.id.number1); numara1.setText(Integer.toString(randomNum1)); //set text for text view TextView numara2 = (TextView) findViewById(R.id.number2); numara2.setText(Integer.toString(randomNum2)); //set text for text view TextView operation = (TextView) findViewById(R.id.operation); operation.setText(operationrandom); //set text for text view } TextView resulthere = (TextView) findViewById(R.id.resulthere); public void checkifright(View v) { EditText tahminsonuc = (EditText) findViewById(R.id.resultsubmit); int tahminedilensonuc = Integer.parseInt(tahminsonuc.getText().toString()); if (tahminedilensonuc == result){ resulthere.setText("You are right congrats!"); } else{ resulthere.setText("You are wrong !!! be careful"); } } }