ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Flutter - Random(함수)
    Flutter 2022. 8. 2. 17:04

    Flutter

     

    Flutter 에서 Random 함수 사용하여 오름차순 내림차순 하는법 

    // 랜덤 오
    for(int i=0; i<a.length; i++){
      for(int j=i+1; j<a.length; j++){
        if(a[i]>a[j]){
          tem = a[i];
          a[i] = a[j];
          a[j] = tem;
        }
      }
      print(a[i]);
    }

    randon 함수를 활용 하여 for문 if문 을 사용 할수 있는데 
    var rnd = Random();
    var a = [];
    변수를 지정해주고 사용을 해야 한다. 안그면 랜덤으로 숫자가 출력이 안되기 때문이다. for문 과 if 문을 작성을 
    다 했고 print로 찍어보면 

    I/flutter (11187) : 13
    I/flutter (11187) : 26
    I/flutter (11187) : 33
    I/flutter (11187) : 36
    I/flutter (11187) : 48
    I/flutter (11187) : 53
    I/flutter (11187) : 80
    I/flutter (11187) : 81

    이렇게 출력이 된다. 내

    림차순은 일 경우 오름차순과 같이 똑같이 해주면 된다. 코드를 다 작성하고 print 를 찍어보면

    //랜덤 내
    for(int i=0; i<a.length; i++){
      for(int j=i+1; j<a.length; j++){
        if(a[j]>a[i]){
          tem = a[j];
          a[j] = a[i];
          a[i] = tem;
        }
      }
      print(a[i]);
    }

    I/flutter (11187): 81
    I/flutter (11187): 80
    I/flutter (11187): 53
    I/flutter (11187): 48
    I/flutter (11187): 46
    I/flutter (11187): 36
    I/flutter (11187): 33
    I/flutter (11187): 26
    I/flutter (11187): 13

    이렇게 출력이 된다.

     

Designed by Tistory.