Android/2012.04월 강좌
3일차 1~45 까지 로또
Bohemian life
2012. 4. 30. 12:29
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff0000" android:text="첫번째번호" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#8df220" android:text="두번째번호" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#603330" android:text="3번째번호" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#0000ff" android:text="4번째번호" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#02ff00" android:text="5번째번호" /> </LinearLayout> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="번호 뽑기" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:maxLength="2" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:maxLength="2" /> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:maxLength="2" /> <EditText android:id="@+id/editText4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:maxLength="2" /> <EditText android:id="@+id/editText5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:maxLength="2" /> </LinearLayout> <TextView android:id="@+id/outcome" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
package com.lotto; import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class LottoActivity extends Activity { TextView tv1,tv2,tv3,tv4,tv5;//로또 추첨번호 뷰 TextView outcome; Button btn1; EditText edit1,edit2,edit3,edit4,edit5; int[] selects=new int[5];//내가선택한 번호 넣을 배열 int[] lotto=new int[5];//당첨 번호 배열 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //클래스 변수의 초기화 tv1=(TextView) this.findViewById(R.id.textView1); tv2=(TextView) this.findViewById(R.id.textView2); tv3=(TextView) this.findViewById(R.id.textView3); tv4=(TextView) this.findViewById(R.id.textView4); tv5=(TextView) this.findViewById(R.id.textView5); btn1=(Button) this.findViewById(R.id.button1); outcome = (TextView)findViewById(R.id.outcome); //사용자가 뽑은 로또번호를 가져오기 위해서 edit1=(EditText)findViewById(R.id.editText1); edit2=(EditText)findViewById(R.id.editText2); edit3=(EditText)findViewById(R.id.editText3); edit4=(EditText)findViewById(R.id.editText4); edit5=(EditText)findViewById(R.id.editText5); /* btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } });*/ MyListener listener = new MyListener(); btn1.setOnClickListener(listener); } int count=0; Random r=new Random(); class MyListener implements OnClickListener{ int ballNumber;//뽑은 번호 @Override public void onClick(View v) { //r.nextInt();//랜덤한 int값을 리턴해줍니다. //r.nextInt(20);//20이하의 랜덤함 int를 리턴해준다. count++; ballNumber=r.nextInt(45)+1; //중복 안되게 처리하자 if(count<6){//count가 6일때는 하지 않기 for(int i=1;i<count;i++){ if(lotto[i]==ballNumber){//에러 범위 ballNumber = r.nextInt(45)+1; i=0;//처음부터 다시 시작 Toast.makeText(LottoActivity.this, "중복", 0).show(); Log.i("중복","중복"); } } } switch(count){ case 1: lotto[0]=ballNumber; tv1.setText(ballNumber+"번"); break; case 2: lotto[1]=ballNumber; tv2.setText(ballNumber+"번"); break; case 3: lotto[2]=ballNumber; tv3.setText(ballNumber+"번"); break; case 4: lotto[3]=ballNumber; tv4.setText(ballNumber+"번"); break; case 5: lotto[4]=ballNumber; tv5.setText(ballNumber+"번"); //사용자가 입력한 번호5개 가져와서 //selects배열에 값넣기 //Editable ->String String no10 = edit1.getText().toString(); String no20 = edit2.getText().toString(); String no30 = edit3.getText().toString(); String no40 = edit4.getText().toString(); String no50 = edit5.getText().toString(); //String -> int int no1=Integer.parseInt(no10); int no2=Integer.parseInt(no20); int no3=Integer.parseInt(no30); int no4=Integer.parseInt(no40); int no5=Integer.parseInt(no50); selects[0] = no1;// 내가 뽑은 번호배열 selects selects[1] = no2; selects[2] = no3; selects[3] = no4; selects[4] = no5; int success=0; //selects 내번호와 lotto 당첨 번호를 비교하기 for(int i=0;i<selects.length;i++){ for(int j=0;j<lotto.length;j++){ //selects[0];//내 첫번째 번호 if(selects[i]==lotto[j]){ success++; } } } outcome.setText(success+"개 맞았다."); break; default://재도전 하기 구현 count=0; tv1.setText(""); tv2.setText(""); tv3.setText(""); tv4.setText(""); tv5.setText(""); } } } }