TextView,EditText,Buttonを使った問題。
Q1
0〜99の値を一つランダムに生成し、その数字を当てるゲームを作成せよ。
入力した値よりも正解が小さい場合は
「もっと下だよ」
入力した値よりも大きい場合は
「もっと上だよ」
正解の場合は
「正解!」
と出力するものとする。
NEW GAMEボタンも配置し、繰り返しゲームをできるようにすること。
詳しくは実行例を参照のこと
[実行例]
スタート画面

数字を入力し、CHECKボタンを押すと下にヒントが表示される。

正解すると「正解!」と表示

NEW GAMEボタンを押すと新しいゲーム開始

●activity_main.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:layout_width = "match_parent" |
04 | android:layout_height = "match_parent" > |
07 | android:id = "@+id/textView" |
08 | android:layout_width = "wrap_content" |
09 | android:layout_height = "wrap_content" |
10 | android:text = "いくつかな?(0~99)" |
11 | android:layout_marginTop = "33dp" |
12 | android:layout_alignParentTop = "true" |
13 | android:layout_centerHorizontal = "true" /> |
16 | android:id = "@+id/etNum" |
17 | android:layout_width = "wrap_content" |
18 | android:layout_height = "wrap_content" |
19 | android:layout_alignStart = "@+id/textView" |
20 | android:layout_below = "@+id/textView" |
21 | android:layout_marginTop = "12dp" |
23 | android:inputType = "number" |
24 | android:layout_alignEnd = "@+id/textView" /> |
27 | android:id = "@+id/button" |
28 | android:layout_width = "match_parent" |
29 | android:layout_height = "wrap_content" |
30 | android:layout_alignParentStart = "true" |
31 | android:layout_below = "@+id/etNum" |
32 | android:layout_marginTop = "17dp" |
33 | android:onClick = "btCheck" |
34 | android:text = "Check!" /> |
37 | android:id = "@+id/tvResult" |
38 | android:layout_width = "wrap_content" |
39 | android:layout_height = "wrap_content" |
40 | android:layout_below = "@+id/button" |
41 | android:layout_centerHorizontal = "true" |
42 | android:layout_marginTop = "27dp" |
43 | android:textAlignment = "center" |
44 | android:textSize = "18sp" /> |
47 | android:id = "@+id/button2" |
48 | android:layout_width = "wrap_content" |
49 | android:layout_height = "wrap_content" |
50 | android:layout_centerHorizontal = "true" |
51 | android:layout_centerVertical = "true" |
52 | android:onClick = "btNewGame" |
53 | android:text = "New Game" /> |
●MainActivity.java
01 | import android.os.Bundle; |
02 | import android.support.v7.app.AppCompatActivity; |
03 | import android.view.View; |
04 | import android.widget.EditText; |
05 | import android.widget.TextView; |
07 | import java.util.Random; |
09 | public class MainActivity extends AppCompatActivity { |
10 | private Random r= new Random(); |
11 | private EditText etNum; |
12 | private TextView tvResult; |
16 | protected void onCreate(Bundle savedInstanceState) { |
17 | super .onCreate(savedInstanceState); |
18 | setContentView(R.layout.activity_main); |
19 | etNum=(EditText)findViewById(R.id.etNum); |
20 | tvResult=(TextView)findViewById(R.id.tvResult); |
25 | answer=r.nextInt( 100 ); |
29 | public void btCheck(View v){ |
30 | int input=Integer.parseInt(etNum.getText().toString()); |
34 | } else if (input < answer){ |
39 | tvResult.setText(msg); |
41 | public void btNewGame(View v){ |
コメント