728x90
package kr.co.aiai.app;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity9 extends AppCompatActivity {
String com = "897";
EditText et;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main9);
et = findViewById(R.id.et);
tv = findViewById(R.id.tv);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myclick();
}
});
ranC();
}
public void ranC(){
int[] arr = {1,2,3,4,5,6,7,8,9};
for (int i = 0; i <= 100; i++) {
int rnd = (int) (Math.random() * 9);
int a = arr[0];
arr[0] = arr[rnd];
arr[rnd] = a;
}
com = arr[0]+""+arr[1]+""+arr[2];
Log.d("test",com);
}
public int getS(String com, String mine){
int ret = 0;
String m1 = mine.substring(0,1);
String m2 = mine.substring(1,2);
String m3 = mine.substring(2,3);
String c1 = com.substring(0,1);
String c2 = com.substring(1,2);
String c3 = com.substring(2,3);
if(m1.equals(c1)) ret++;
if(m2.equals(c2)) ret++;
if(m3.equals(c3)) ret++;
return ret;
}
public int getB(String com, String mine){
int ret = 0;
String m1 = mine.substring(0,1);
String m2 = mine.substring(1,2);
String m3 = mine.substring(2,3);
String c1 = com.substring(0,1);
String c2 = com.substring(1,2);
String c3 = com.substring(2,3);
if(m1.equals(c2) || m1.equals(c3) ) ret++;
if(m2.equals(c1) || m2.equals(c3)) ret++;
if(m3.equals(c1) || m2.equals(c2)) ret++;
return ret;
}
public void myclick() {
String mine = et.getText().toString();
int s = getS(com, mine);
int b = getB(com, mine);
String str_old = tv.getText().toString();
String str_new = mine + " "+s+"s"+b+"b"+"\n";
tv.setText(str_new + str_old);
et.setText("");
if(s==3){
Toast toast = Toast.makeText(getApplicationContext(),mine+"정답입니다.", Toast.LENGTH_SHORT);
toast.show();
}
Log.d("test",mine +" " + s + "s" + b + "b");
}
}
<?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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="야구수:"
android:textSize="25dp" />
<EditText
android:id="@+id/et"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="게임하기" />
</LinearLayout>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
'안드로이드 > 안드로이드 연습' 카테고리의 다른 글
안드로이드(자바) 전화번호 찍기 예제 (0) | 2023.06.27 |
---|---|
안드로이드(자바) 별찍기 예제 (0) | 2023.06.27 |
안드로이드(자바) 구구단 출력 예제 (0) | 2023.06.27 |
안드로이드(자바) 홀/짝 맞추기 예제 (0) | 2023.06.27 |
안드로이드(자바) 로또 출력 예제 (0) | 2023.06.27 |