- 공유 링크 만들기
- X
- 이메일
- 기타 앱
각 자리 숫자가 중복 되면 안된다고 한다면 -> 3자리 숫자를 한번에 뽑는게 가능하려면 어떻게 해야하지?? 늘 random method를 사용하면 중복되는 숫자도 나오는데.
복습.
/랜덤수 발생
System.out.println((int)(math.random()*100) + 1);
// Math.random() 은 0.0 부터 1.0미만의 수를 발생시켜 준다.
0 ----------------------------1 사이의 수.
그러면 math.random 이 숫자에 우선 100을 곱해주는 것에 정수만을 구하고 1을 더해주면
1부터 100까지의 수가 나온다.
for( int i = 1 ; i =< 3 ; i++){
int a = (int)(Math.random()*10) ;
int b = (int)(Math.random()*10) ;
int c = (int)(Math.random()*10) ;
while ( a != b && b != c && a != c ) {
---> 내가 생각한것은 랜덤숫자발생 후 while로 조건을 만들어줘서 a,b,c를 중복되지 않게 해주는
것이다. 여기서의 문제는 과연 저 while이 제대로 작동하지 않으면 -> 다시
올라가서 a,b,c를 다시뽑는 것인가? 하는것.
package doctor;
import java.util.Scanner;
public class Home {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a = (int)(Math.random()*10) ;
int b = (int)(Math.random()*10) ;
int c = (int)(Math.random()*10) ;
while ( a != b && b != c && a != c ) {
System.out.print("100의 자리 숫자를 입력하시오 >");
int x = Integer.parseInt(sc.nextLine());
System.out.print("100의 자리 숫자와 다른 10의자리 숫자를 입력하시오 >");
int y = Integer.parseInt(sc.nextLine());
System.out.print("100의자리와 10의 자리 숫자와 다른 1의 자리 숫자를 입력하시오 >");
int z = Integer.parseInt(sc.nextLine());
do {
int st = 0;
int ba = 0;
int o = 0;
if ( x == a ) {
st += 1;
}else if ( x == b ){
ba += 1;
}else if ( x == c ){
ba += 1;
}else {
o += 1; }
if ( y == a ) {
ba += 1;
}else if ( y == b) {
st += 1;
}else if ( y == c ){
ba += 1;
}else {
o += 1; }
if ( z == a ) {
ba += 1;
}else if ( z == b ){
ba += 1;
}else if ( z == c ){
st += 1;
}else {
o += 1 ;}
System.out.print ( "a" + "b" + "c" + " : " + a + " strike " + "," + b + " ball " + " " + o + "out" );
} while ( (a * 100 + b * 10 + c * 1) != (x * 100 + y * 10 + z * 1) );
}
}
}
내코드 :
package doctor;
import java.util.Scanner;
public class Home {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a = (int)(Math.random()*10) ;
int b = (int)(Math.random()*10) ;
int c = (int)(Math.random()*10) ;
while ( a != b && b != c && a != c ) {
do {
System.out.print("100의 자리 숫자를 입력하시오 >");
int x = Integer.parseInt(sc.nextLine());
System.out.print("100의 자리 숫자와 다른 10의자리 숫자를 입력하시오 >");
int y = Integer.parseInt(sc.nextLine());
System.out.print("100의자리와 10의 자리 숫자와 다른 1의 자리 숫자를 입력하시오 >");
int z = Integer.parseInt(sc.nextLine());
int st = 0;
int ba = 0;
int o = 0;
if ( x == a ) {
st += 1;
}else if ( x == b ){
ba += 1;
}else if ( x == c ){
ba += 1;
}else {
o += 1; }
if ( y == a ) {
ba += 1;
}else if ( y == b) {
st += 1;
}else if ( y == c ){
ba += 1;
}else {
o += 1; }
if ( z == a ) {
ba += 1;
}else if ( z == b ){
ba += 1;
}else if ( z == c ){
st += 1;
}else {
o += 1 ;}
System.out.print ( "a" + "b" + "c" + " : " + a + " strike " + "," + b + " ball " + " " + o + "out" );
} while ( (a * 100 + b * 10 + c * 1) != (x * 100 + y * 10 + z * 1) );
}
System.out.print(a * 100 + b * 10 + c * 1) ;
}
}
여러사람의 코드로 공부하기 .
댓글
댓글 쓰기