Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.higherorlower;
- 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 {
- public void Go(View view) {
- EditText enteredNumber = findViewById(R.id.enteredNumber);
- String entryAsString = enteredNumber.getText().toString();
- // int entryAsInt = Integer.parseInt(entryAsString);
- if (!entryAsString.equals(""))
- if (Integer.parseInt(entryAsString) >= 0 && Integer.parseInt(entryAsString) <= 20)
- if (Integer.parseInt(entryAsString) > randomNum) {
- Toast.makeText(this, "Choose a lesser number", Toast.LENGTH_SHORT).show();
- enteredNumber.setText("");
- } else if (Integer.parseInt(entryAsString) < randomNum) {
- Toast.makeText(this, "Choose a higher number", Toast.LENGTH_SHORT).show();
- enteredNumber.setText("");
- } else {
- Toast.makeText(this, "Bingo! " + randomNum + " was the number I was thinking", Toast.LENGTH_SHORT).show();
- random1();
- enteredNumber.setText("");
- }
- else {
- Toast.makeText(this, "You are guessing number out of the limit!", Toast.LENGTH_LONG).show();
- enteredNumber.setText("");
- }
- else {
- // Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
- }
- }
- int randomNum;
- public void random1() {
- Random r = new Random();
- randomNum = r.nextInt(20);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- random1();
- }
- }
- ******************************************************************************
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:onClick="Go"
- tools:context=".MainActivity">
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="37dp"
- android:layout_marginTop="63dp"
- android:layout_marginEnd="36dp"
- android:layout_marginBottom="558dp"
- android:text="@string/i_have_a_number_in_my_mind_between_0_and_20_can_you_guess_it"
- android:textAlignment="center"
- android:textSize="16sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <EditText
- android:id="@+id/enteredNumber"
- android:layout_width="329dp"
- android:layout_height="51dp"
- android:layout_marginStart="10dp"
- android:layout_marginEnd="10dp"
- android:layout_marginBottom="452dp"
- android:ems="10"
- android:hint="@string/enter_the_number_you_re_guessing"
- android:inputType="number"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.545"
- app:layout_constraintStart_toStartOf="parent"
- android:importantForAutofill="no" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="135dp"
- android:layout_marginTop="23dp"
- android:layout_marginEnd="137dp"
- android:layout_marginBottom="381dp"
- android:clickable="false"
- android:onClick="Go"
- android:soundEffectsEnabled="false"
- android:text="@string/enter"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/enteredNumber" />
- </androidx.constraintlayout.widget.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement