'2017/10/18'에 해당되는 글 1건

  1. 2017.10.18 자바 랜덤 숫자 발생 정렬 후 반환 353
IT/java2017. 10. 18. 00:14
반응형



자바 랜덤 숫자 발생 정렬 후 반환 



import java.util.Arrays;

import java.util.Random;



/**

 * Created by buster on 17. 9. 14.

 */



public class randomNum {


    public int[] lotArray(int input1, int input2) {


        int[] num = new int[input1];

        int lottoMax = input2;

        int MAX = input1;

        int xnum;

        int icount, j;



        Random rand = new Random();

//         로또 번호 발생 및  중복 제거


        for (icount = 0; icount < MAX; icount++) {

            xnum = rand.nextInt(lottoMax) + 1;

            num[icount] = xnum;

            for (j = 0; j < icount; j++) {

                if (num[icount] == num[j]) {

                    xnum = rand.nextInt(lottoMax) + 1;

                    num[icount] = xnum;

                    icount = icount - 1;

                    break;

                }

            }

        }

//   오름 차순으로  정렬 및 리턴 어레이 전달

        Arrays.sort(num);

        return num;

    }

}

반응형

'IT > java' 카테고리의 다른 글

java split() 문자 자르기  (684) 2017.10.27
자바 자문 자답 DecimalFormat 에러 발생  (627) 2017.10.21
Posted by Dream Come True