quilt code

[kotlin] 로또 번호 생성 본문

daily/Android

[kotlin] 로또 번호 생성

김뱅쇼 2023. 5. 26. 15:29

 

일단 function이 제대로 작동하는지 확인해보기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package kr.co.aiai.app
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import java.util.*
 
// : 상속
class MainActivity3 : AppCompatActivity() {
    //전역변수
    var tv1:TextView? = null;
    var tv2:TextView? = null;
    var tv3:TextView? = null;
    var tv4:TextView? = null;
    var tv5:TextView? = null;
    var tv6:TextView? = null;
 
    //메소드
    //fun : 파이썬의 def
    override fun onCreate(savedInstanceState: Bundle?) {
        //super:자기 조상 호출
        super.onCreate(savedInstanceState)
        //화면으로 넘어가는 명령어
        setContentView(R.layout.activity_main3)
 
        //레이아웃 선언
        tv1= findViewById<TextView>(R.id.tv1)
        tv2= findViewById<TextView>(R.id.tv2)
        tv3= findViewById<TextView>(R.id.tv3)
        tv4= findViewById<TextView>(R.id.tv4)
        tv5= findViewById<TextView>(R.id.tv5)
        tv6= findViewById<TextView>(R.id.tv6)
 
        val btn : Button = findViewById(R.id.btn)
 
        //버튼 클릭할때마다 로또 생성성
        btn.setOnClickListener{
            myclick()
 
        }
 
    }
    fun myclick(){
        tv1?.setText("5")
        tv2?.setText("6")
        tv3?.setText("7")
        tv4?.setText("8")
        tv5?.setText("9")
        tv6?.setText("0")
 
 
 
    }
 
}
cs



^ 번호가 설정한대로 출력이 안되지 했는데



내가 디자인에서 순서를 엉망으로 해서 그런거였음...
tv1?.setText("5")
tv2?.setText("6")
tv3?.setText("7")
tv4?.setText("8")
tv5?.setText("9")
tv6?.setText("0")
val random = Random()
val list = mutableListOf<Int>()

while (list.size<6){
    var randomNumber = random.nextInt(45)+1
    if(list.contains(randomNumber)){
        continue
    }
    list.add(randomNumber)

}
print(list)
//        val set = mutableSetOf<Int>()
//
//        while(set.size < 6){
//            set.add(1..45).random())
//
//        }
//
//        println(set.sorted())

        //일단 찍어보기
//      tv1?.setText("5")
//      tv2?.setText("6")
//      tv3?.setText("7")
//      tv4?.setText("8")
//      tv5?.setText("9")
//      tv6?.setText("0")

 

    val random = Random()
    val list = mutableListOf<Int>()

    while (list.size<6){
        val tv1 = random.nextInt(45)+1
        if(list.contains(tv1)) {
            continue
        }
        list.add(tv1)

}

 

 

***생각하는 과정***

 

1. 배열 선언하고 1-45까지  : listOf사용

temp 자리 바꾸기

2. 섞고 (잘섞였는지 확인) : shuffled 함수 사용

 

>>잘 섞였다!

 

 

3. textView에 넣기

 

코틀린 배열선언

숫자 섞고 

랜덤으로 뽑힌게  출력이 되느냐

 

tv1?.append(lotto[0].toString());

 

 

set text

 

 

for문을 돌리면 코드가 지저분해짐....

 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package kr.co.aiai.app
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import java.util.*
 
// : 상속
class MainActivity3 : AppCompatActivity() {
    //전역변수
    var tv1:TextView? = null;
    var tv2:TextView? = null;
    var tv3:TextView? = null;
    var tv4:TextView? = null;
    var tv5:TextView? = null;
    var tv6:TextView? = null;
 
 
 
    //메소드
    //fun : 파이썬의 def
    override fun onCreate(savedInstanceState: Bundle?) {
        //super:자기 조상 호출
        super.onCreate(savedInstanceState)
        //화면으로 넘어가는 명령어
        setContentView(R.layout.activity_main3)
 
        //레이아웃 선언
        tv1= findViewById<TextView>(R.id.tv1)
        tv2= findViewById<TextView>(R.id.tv2)
        tv3= findViewById<TextView>(R.id.tv3)
        tv4= findViewById<TextView>(R.id.tv4)
        tv5= findViewById<TextView>(R.id.tv5)
        tv6= findViewById<TextView>(R.id.tv6)
 
        val btn : Button = findViewById(R.id.btn)
 
        //버튼 클릭할때마다 로또 생성성
        btn.setOnClickListener{
            myclick()
 
        }
 
    }
 
 
    fun myclick(){
        var lotto = listOf( 1,2,3,4,5       ,6,7,8,9,10,
                            11,12,13,14,15  ,16,17,18,19,20,
                            21,22,23,24,25  ,26,27,28,29,30,
                            31,32,33,34,35  ,36,37,38,39,40,
                            41,42,43,44,45)
 
        lotto = lotto.shuffled()
 
        tv1?.append(lotto[0].toString());
        tv2?.append(lotto[1].toString());
        tv3?.append(lotto[2].toString());
        tv4?.append(lotto[3].toString());
        tv5?.append(lotto[4].toString());
        tv6?.append(lotto[5].toString());
 
//        tv1?.text=lotto[0].toString()
//        tv2?.text=lotto[1].toString()
//        tv3?.text=lotto[2].toString()
//        tv4?.text=lotto[3].toString()
//        tv5?.text=lotto[4].toString()
//        tv6?.text=lotto[5].toString()
 
 
    }
 
}
cs

이렇게 하면




이렇게 뜸... 
why? 




append 때문..
append는 값 추가여서 저렇게 나온다
append를 .text 나 setText로 바꿔보자


 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package kr.co.aiai.app
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import java.util.*
 
// : 상속
class MainActivity3 : AppCompatActivity() {
    //전역변수
    var tv1:TextView? = null;
    var tv2:TextView? = null;
    var tv3:TextView? = null;
    var tv4:TextView? = null;
    var tv5:TextView? = null;
    var tv6:TextView? = null;
 
 
 
    //메소드
    //fun : 파이썬의 def
    override fun onCreate(savedInstanceState: Bundle?) {
        //super:자기 조상 호출
        super.onCreate(savedInstanceState)
        //화면으로 넘어가는 명령어
        setContentView(R.layout.activity_main3)
 
        //레이아웃 선언
        tv1= findViewById<TextView>(R.id.tv1)
        tv2= findViewById<TextView>(R.id.tv2)
        tv3= findViewById<TextView>(R.id.tv3)
        tv4= findViewById<TextView>(R.id.tv4)
        tv5= findViewById<TextView>(R.id.tv5)
        tv6= findViewById<TextView>(R.id.tv6)
 
        val btn : Button = findViewById(R.id.btn)
 
        //버튼 클릭할때마다 로또 생성성
        btn.setOnClickListener{
            myclick()
 
        }
 
    }
 
 
    fun myclick(){
        var lotto = listOf( 1,2,3,4,5       ,6,7,8,9,10,
                            11,12,13,14,15  ,16,17,18,19,20,
                            21,22,23,24,25  ,26,27,28,29,30,
                            31,32,33,34,35  ,36,37,38,39,40,
                            41,42,43,44,45)
 
        lotto = lotto.shuffled()
 
//        tv1?.append(lotto[0].toString());
//        tv2?.append(lotto[1].toString());
//        tv3?.append(lotto[2].toString());
//        tv4?.append(lotto[3].toString());
//        tv5?.append(lotto[4].toString());
//        tv6?.append(lotto[5].toString());
 
        tv1?.text=lotto[0].toString()
        tv2?.text=lotto[1].toString()
        tv3?.text=lotto[2].toString()
        tv4?.text=lotto[3].toString()
        tv5?.text=lotto[4].toString()
        tv6?.text=lotto[5].toString()
 
 
    }
 
}
cs






출력하는 부분을 .text로 바꾸었더니 원하는 모양대로 나왔다

 

'daily > Android' 카테고리의 다른 글

[kotlin] 버튼 클릭 이벤트 (2)  (0) 2023.05.26
[kotlin] 버튼 클릭 이벤트 (1)  (0) 2023.05.26