Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.randomnumber;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity {
- Button sub;
- EditText num;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- sub = findViewById(R.id.sub);
- num = findViewById(R.id.num);
- sub.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String temp = num.getText().toString();
- int num = Integer.parseInt(temp);
- // random number
- Random rand = new Random();
- int r = rand.nextInt(5);
- if (num == r) {
- Toast.makeText(getApplicationContext(),"Guess Matched! " + "\nRandom Value:" + String.valueOf(r) + "\nInput Value:" + String.valueOf(num), Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(getApplicationContext(),"Guess Not Matched!"+ "\nRandom Value:" + String.valueOf(r) + "\nInput Value:" + String.valueOf(num), Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement