안드로이드 개발 질문/답변
(글 수 45,052)
<?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:weightSum="1" android:orientation="vertical"> <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:id="@+id/button1"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:id="@+id/button2"></Button> <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent" android:orientation="horizontal"> <Button android:text="Button" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Button" android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Button" android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_width="match_parent" android:orientation="horizontal"> <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start"></Button> </LinearLayout> </LinearLayout>
package com.android.Lotto2;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button;
public class Lotto2 extends Activity {
/** Called when the activity is first created. */
Button[] buttons;
int[]numbers;
//int number = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int[] buttonIds = {R.id.button1, R.id.button2, R.id.button3,
R.id.button4, R.id.button5, R.id.button6};
buttons = new Button[6];
numbers = new int[6];
for(int i = 0; i < 6; i++){
buttons[i] = (Button)findViewById(buttonIds[i]);
}
Button btn = (Button)findViewById(R.id.button7);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
do{
for(int i = 0; i < 6; i++){
int number = (int)(Math.random()*45) + 1;
numbers[i] = number;
}
}while(isDuplicate());
sort();
for(int i =0; i < 6; i++){
buttons[i].setText(String.valueOf(numbers[i]));
}
}
});
}
//정렬
public void sort(){
for(int i = 0; i < 5; i++){
for(int j = 0; j < 6; j++){
if(numbers[i] > numbers[j]){
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
}
//중복제거
public boolean isDuplicate(){
for(int i = 0; i < 5; i++)
for(int j = i+1; j < 6; i++)
if(numbers[i] == numbers[j])
return true;
return false;
}
}로그켓 이미지도 첨부합니다.
로또소스인데요. 아무 에러도 없고 로그켓에서도 얌전합니다.
그런데 start버튼 클릭을 하면 노트북 cpu펜이 사정없이 돌아갑니다. SDK는 다운먹은거 같구요....
왜이러는 건가요? 코드에 무슨 문제가 있겠죠??
도저히 문제점을 못찾겠네요....




중복제거 for문 에러요
for
(intj = i+1; j <6; i++)i++이 아니라 j++이겠죠