자바 로또 과제 질문입니다.
-
게시물 수정 , 삭제는 로그인 필요
import java.util.*;
class Test {
public static void main(String args[]) {
int num[] = new int[6]; //배열 생성
for (int i = 0; i < 6; i++) {
num[i] = (int) (Math.random() * 45) + 1;//배열에 1~45의 숫자 랜덤으로 삽입
for (int j = 0; j < i; j++) {
if (num[i] == num[j]) { // 같은 숫자가 발견되면
num[i] = (int) (Math.random() * 45) + 1;//같은 숫자라면 숫자 다시 생성하여 삽입
i = i - 1; // 배열위치 이동
//break; 없애는게 맞을듯
}
}
}
for (int i = 0; i < 6; i++) {
System.out.println(num[i]); // 출력
}
}
class LottoTest {
public void main(String args[]){
Scanner scan=new Scanner(System.in);
while(true){
System.out.print("로또 한번 찍어볼까?(시작:아무키나 입력, 종료:q) ");
if(scan.next().equals("q"))//q가 입력되면 종료
break;
List<Integer> input=new ArrayList<Integer>();//Integer 형태만 들어갈수있는 ArrayList 생성
int n;
for(int i=1;i<7;i++){
System.out.print((i)+"번째 숫자입력:");
n=scan.nextInt();//로또번호 사용자 입력
if(input.contains(n)){
System.out.println("중복입력할수 없습니다");
i--;
}else if(n>45){
System.out.println("45까지만 입력해주세요.");
i--;
}else
input.add(n);//ArrayList 에 입력받은 숫자 삽입
}
LottoTest lotto = new LottoTest();
System.out.println("로또 번호: "+lotto.getResult());
System.out.println("입력 번호: "+input.toString());
int result=lotto.check(input);//로또 당첨여부 체크
if(result==1)
System.out.println("당첨되지 않았습니다.");
else
System.out.printf("%d 등에 당첨되었습니다\n",result);
}
}
private String getResult() {
return null;
}
private int check(List<Integer> input) {
return 1;
}
}
}
이런 식으로 구하되,
prn()으로 출력하라고 그랬고,
클래스는 lotto와 lottotest 두개를 구하는 것입니다.
lottotest는 돈을 입력받아서 돈을 지불한만큼 lotto번호를 출력할 수 있게 하는 프로그램입니다.
저도 이리저리 해보는데 잘 몰라서 질문드렸습니다.
많은 답변 부탁드립니다.
import java.util.*;
class Test {
public static void main(String args[]) {
int num[] = new int[6]; //배열 생성
for (int i = 0; i < 6; i++) {
num[i] = (int) (Math.random() * 45) + 1;//배열에 1~45의 숫자 랜덤으로 삽입
for (int j = 0; j < i; j++) {
if (num[i] == num[j]) { // 같은 숫자가 발견되면
num[i] = (int) (Math.random() * 45) + 1;//같은 숫자라면 숫자 다시 생성하여 삽입
i = i - 1; // 배열위치 이동
//break; 없애는게 맞을듯
}
}
}
for (int i = 0; i < 6; i++) {
System.out.println(num[i]); // 출력
}
}
class LottoTest {
public void main(String args[]){
Scanner scan=new Scanner(System.in);
while(true){
System.out.print("로또 한번 찍어볼까?(시작:아무키나 입력, 종료:q) ");
if(scan.next().equals("q"))//q가 입력되면 종료
break;
List<Integer> input=new ArrayList<Integer>();//Integer 형태만 들어갈수있는 ArrayList 생성
int n;
for(int i=1;i<7;i++){
System.out.print((i)+"번째 숫자입력:");
n=scan.nextInt();//로또번호 사용자 입력
if(input.contains(n)){
System.out.println("중복입력할수 없습니다");
i--;
}else if(n>45){
System.out.println("45까지만 입력해주세요.");
i--;
}else
input.add(n);//ArrayList 에 입력받은 숫자 삽입
}
LottoTest lotto = new LottoTest();
System.out.println("로또 번호: "+lotto.getResult());
System.out.println("입력 번호: "+input.toString());
int result=lotto.check(input);//로또 당첨여부 체크
if(result==1)
System.out.println("당첨되지 않았습니다.");
else
System.out.printf("%d 등에 당첨되었습니다\n",result);
}
}
private String getResult() {
return null;
}
private int check(List<Integer> input) {
return 1;
}
}
}
이런 식으로 구하되,
prn()으로 출력하라고 그랬고,
클래스는 lotto와 lottotest 두개를 구하는 것입니다.
lottotest는 돈을 입력받아서 돈을 지불한만큼 lotto번호를 출력할 수 있게 하는 프로그램입니다.
저도 이리저리 해보는데 잘 몰라서 질문드렸습니다.
많은 답변 부탁드립니다.
#자바 로또 중복제거 #자바 로또 번호 생성 #자바 로또 당첨 프로그램 #자바 로또 랜덤 #자바 로또 오름차순 #자바 로또 게임 #자바 로또 보너스 번호 #자바 로또번호 오름차순 #자바 로또번호 추출 #자바 로또 프로그램 소스