2008년 7월 20일 일요일

java Scanner test

자바 프로그래밍으로 로또 번호 생성/결과 맞추기




public class lotto
{
public static void main( String[] args )
{
int[] r = getUserNumbers();// 입력 함수 호출
for(int i = 0; i < r.length; i++) {//받은 숫자 출력
System.out.println(r[i]);
}
}

public static int[] getUserNumbers() {


boolean[] chk =new boolean[49];// 중복입력 판단용 배열
int List[] = new int[6];
int temp[] = new int[List.length];
int num;
Scanner input = new Scanner(System.in);
boolean isValidate ;

for(int i = 0; i < List.length; i++) {

System.out.print("Enter 6 different numbers bewtween 1 and 49:");
num = input.nextInt();
isValidate = num > 0 && num < 50;
// chk[num]가 참이면 중복입력
//거짓이면 입력값을 배열에 저장하고 chk[num]를 참으로 만든다.
if(isValidate && !chk[num]) { List[i] = num; chk[num]=true;}
else --i;
}
return List;//결과를 배열로 return
}

}



문]

메써드와 어레이를 사용해서 로또 복권을 플레이하는거랍니다.우선 결과 맞추기, 자동번호 생성 메써드는 다 되었는데..유저 입력부분에서 1~49번까지만 받고 그리고 중복번호는 입력받지않는 부분에서 헤매게 되네요..'

댓글 없음:

댓글 쓰기

-


Sidewinder


World


FishMusic


LaughingBaby