728x90
728x90

구구단 맞추기

 

<난수생성!> 버튼을 눌러서, 첫번째 칸과 두번째 칸에 2~9 사이의 난수를 만든다
(nextInt(8)의 매개변수는 0~7 사이의 난수를 생성시킨다. 그러므로, 2를 더하면 2~9 사이의 난수가 생성된다.)

난수 발생 코드:  int rand1 = new Random().nextInt(8)+2;

 

3번째 칸에 정답을 넣고 <정답확인!> 버튼을 누른다.

 

코드

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

<LinearLayout
    android:id="@+id/lLay1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">


    <EditText
        android:id="@+id/edt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="x" />

    <EditText
        android:id="@+id/edt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="=" />


    <EditText
        android:id="@+id/edt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

    <Button
        android:id="@+id/btnOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/lLay1"
        android:layout_alignRight="@id/lLay1"
        android:text="정답 확인" />

    <Button
        android:id="@+id/btnRandom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/lLay1"
        android:layout_marginTop="-4dp"
        android:layout_toLeftOf="@id/btnOK"
        android:text="난수 생성" />


    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnOK"
        tools:ignore="UnknownId" />

</RelativeLayout>
package com.example.middletest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText edt1 = (EditText) findViewById(R.id.edt1);
        final EditText edt2 = (EditText) findViewById(R.id.edt2);
        final EditText edt3 = (EditText) findViewById(R.id.edt3);

        final ListView list1 = (ListView) findViewById(R.id.list1);

        Button btnRandom = (Button)findViewById(R.id.btnRandom);
        Button btnOK = (Button)findViewById(R.id.btnOK);

        btnRandom.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                int rand1 = new Random().nextInt(8)*2;
                int rand2 = new Random().nextInt(8)*2;
                edt1.setText(String.valueOf(rand1));
                edt2.setText(String.valueOf(rand2));
            }
        });
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(), 0);

                String str1 = edt1.getText().toString();
                String str2 = edt2.getText().toString();
                String str3 = edt3.getText().toString();
                int a1 = Integer.parseInt(str1);
                int a2 = Integer.parseInt(str2);
                int a3 = Integer.parseInt(str3);

                int a4 = a1*a2;

                if(a3 == a4) //정답
                {
                    Toast.makeText(MainActivity.this, "정답입니다!",
                            Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "틀렸습니다!",
                            Toast.LENGTH_SHORT).show();
                    String[] values = new String[9];
                    for(int i=0; i<9; i++) {
                        values[i] = String.valueOf(a1) + "X" + (i+1) + "=" + (a1*(i+1));
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                            android.R.layout.simple_list_item_1, values);
                    list1.setAdapter(adapter);
                }
            }
        });

    }
}

 

728x90
728x90
블로그 이미지

coding-restaurant

코딩 맛집에 방문해주셔서 감사합니다.

,

v