Advertisement
Shishu

java file for randomnumber

Dec 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. package com.example.randomnumber;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11. import java.util.Random;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15.     Button sub;
  16.     EditText num;
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.  
  23.         sub = findViewById(R.id.sub);
  24.         num = findViewById(R.id.num);
  25.  
  26.         sub.setOnClickListener(new View.OnClickListener() {
  27.             @Override
  28.             public void onClick(View v) {
  29.  
  30.                 String temp = num.getText().toString();
  31.                 int num = Integer.parseInt(temp);
  32.                 // random number
  33.                 Random rand = new Random();
  34.                 int r = rand.nextInt(5);
  35.  
  36.                 if (num == r) {
  37.                     Toast.makeText(getApplicationContext(),"Guess Matched! " + "\nRandom Value:" + String.valueOf(r) + "\nInput Value:" + String.valueOf(num), Toast.LENGTH_SHORT).show();
  38.                 } else {
  39.                     Toast.makeText(getApplicationContext(),"Guess Not Matched!"+ "\nRandom Value:" + String.valueOf(r) + "\nInput Value:" + String.valueOf(num), Toast.LENGTH_SHORT).show();
  40.                 }
  41.  
  42.             }
  43.         });
  44.  
  45.  
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement