// ■じゃんけんプログラム //  guh.png(ぐうの画像), pah.png(ぱーの画像), choki.png(ちょきの画像)を // resフォルダに移動して以下のプログラムをコピペするとよいでしょう。 // なお、package文や、class MainActivity部分, onCreate部分については // 自動生成されたものを使ってください。 package jp.eclipse; import java.util.Random; import android.os.Bundle; import android.app.Activity; import android.graphics.BitmapFactory; import android.graphics.Color; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { final static int GUH = 0; // ぐうを表す定数 final static int CHOKI = 1; // ちょきを表す定数 final static int PAH = 2; // ぱーを表す定数 ImageView[] iv=new ImageView[3]; //じゃんけんの画像。添え字 0:ぐう, 1:ちょき,2:ぱー TextView tv, tvR; //メッセージ用のテキストビュー Random rn=new Random(); //乱数でじゃんけんの手を決める String[] Janken={"ぐう","ちょき","ぱー"}; //じゃんけんの文字列 // 自分 { GUH , CHOKI, PAH } //勝負判定用テーブル int win [] = { CHOKI, PAH , GUH }; //自分が勝つときの相手の手 int ID[] = {R.drawable.guh, R.drawable.choki, R.drawable.pah}; //画像ID int twin, tlose, twl; // 回数 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout LL=new LinearLayout(this); LL.setOrientation(LinearLayout.VERTICAL); setContentView(LL); tv= new TextView(this); tv.setText("     じゃんけんしましょう"); tv.setBackgroundColor(Color.RED); // 勝負表示用テキスト tv.setTextColor(Color.YELLOW); LL.addView(tv); for(int i=0;i<3;i++) { iv[i]=new ImageView(this); //以下、ビットマップの設定 iv[i].setImageBitmap(BitmapFactory.decodeResource(getResources(),ID[i])); LL.addView(iv[i]); iv[i].setOnClickListener(new exClickListener()); } twin=tlose=twl=0; // 対戦成績表示用テキスト tvR= new TextView(this); tvR.setText(" "); tvR.setBackgroundColor(Color.BLUE); tvR.setTextColor(Color.WHITE); LL.addView(tvR); } class exClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO 自動生成されたメソッド・スタブ for(int i=0;i<3; i++) if(v==iv[i]) judge(i); } public void judge(int you){ //じゃんけんの判定 String s="    わたしは,"; // you:相手の手,me:自分の手 int me = rn.nextInt(3); // 自分の手を乱数で決める s += Janken[me]+"。"; // 自分の手を文字列として設定 if (you == me ) {s += "あいこ" ;twl++ ;} else if(you == win[me]) {s += "わたしの勝ち";twin++ ;} else {s += "あなたの勝ち";tlose++;} tv.setText(s + "です。"); // テキストビューに文字列設定 tvR.setText(" あなたの勝ち "+ tlose +" わたしの勝ち "+ twin+ " 引き分け "+twl); } } }