Open In App

Guessing the Number Game using Android Studio

A Simple Guess the number application in which application generates the random number between 1 to 100 and the user has to guess that Number. It is a simple and basic application that uses basic widgets and libraries.
Approach:

Step1: Creating a new project



Step2: Designing the UI

Step3: Working with Java file

Java code for MainActivity.java is:




package com.example.guessthenumber;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    int result;
    static int getRandomNumber(int max, int min)
    {
        return (int)((Math.random()
 * (max - min)) + min);
    }
  
    public void makeToast(String str)
    {
        Toast.makeText(
MainActivity.this
str,
 Toast.LENGTH_SHORT)
.show();
    }
    public void clickFunction(View view)
    {
        int userGuessing;
        EditText variable
 = (EditText)findViewById(
R.id.editId);
        userGuessing
 = Integer.parseInt(
variable
.getText()
.toString());
        if (userGuessing < result) {
  
            makeToast("Think of Higher Number, 
Try Again");
        }
        else if (userGuessing > result) {
            makeToast("Think of Lower Number, 
Try Again");
        }
        else {
            makeToast(
"Congratulations,"
+" You Got the Number");
        }
    }
  
    @Override
    protected void onCreate(
Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        int min = 1;
        int max = 100;
        result = getRandomNumber(min, max);
    }
}

Output:


Article Tags :