출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]
import java.util.Random;

public class RandomnumberGenerator {
	
	public static void main(String[] args) {
		
		Random r = new Random();
		
		System.out.println(r.nextBoolean());	// 논리값두개랜덤출력 t/f
		System.out.println(r.nextInt());		// Integer범위안의 랜덤
		System.out.println(r.nextLong());
		
		System.out.println(r.nextInt(45)); // 범위설정 랜덤 0~44
		System.out.println("이번주 번호예상");
		for(int i=0; i<6; i++) {
			System.out.print(r.nextInt(46)+"\t");
		}
		
		
		Random r2=new Random(10); //seed가잇어서 한번 나온 랜덤수가 계속 똑같이 출력됌
		System.out.println("이번주 번호예상");
		for(int i=0; i<6; i++) {
			System.out.print(r2.nextInt(46)+"\t");
		}
		
		System.out.println();
		
		System.out.println("System.currentTimeMillis()  : "+ System.currentTimeMillis()); // 바뀜
		System.out.println("System.nanoTime()  : " +System.nanoTime());
		Random r3=new Random(System.currentTimeMillis());
		System.out.println("이번주 번호예상");
		for(int i=0; i<6; i++) {
			System.out.print(r3.nextInt(46)+"\t");
		}
	
	}

}
​
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
        int r;
        r = (int) (Math.random() * 10); // 0~9
        // Math.random은 double형이기 때문
        // * n 은 0 ~ (n-1)까지 랜덤이라는 뜻
 
 
        
 
        
         // 랜덤으로 돌아다니는 몹(?)을 만드는 것
        
    int x;
    int y;
        
        x=(int)(Math.random()*3-1 ;
        y=(int)(Math.random()*3-1 ;
        // x = -1, 0, 1
        // y = -1, 0, 1
    
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        
        Scanner sc = new Scanner(System.in);
        int a ; // 랜덤으로 정해질 숫자
        int b ; // 유저로부터 입력받을 숫자
        a=(int)(Math.random()*100)+1;
        
        for(int i=0 ; i<10 ; i++){ // 10번의 기회만 줄것
            System.out.print("숫자를 입력하세요 (1~100): ");
            b=sc.nextInt();
                if(b>a) {
                    System.out.println("정답이 입력한 숫자보다 작습니다");}
                else if(b<a) {
                    System.out.println("정답이 입력한 숫자보다 큽니다");}
                else {
                    System.out.println("정답입니다");
                    break;}
                if(i==9) {
                    System.out.println("10번의 기회를 모두 사용하셨습니다.");
                }
            
    
cs

 

- 여러가지 요소 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Scanner sc = new Scanner(System.in);
        
        /*
            숫자 찾기 게임
            
            random number
            범위: 1 ~ 100 -> number(71)            
                        
            user 입력: 50
            -> 너무 작습니다
            user 입력: 75
            -> 너무 큽니다
            user 입력: 65
            -> 너무 작습니다
            user 입력: 70
            -> 너무 작습니다
 
        */
        
        // 1.변수의 선언
        int r_num, u_num;
        boolean clear;
        String s_num;
        
        clear = false;
        
        // 2.랜덤 수의 산출(1 ~ 100)
        r_num = (int)(Math.random() * 100+ 1;
        System.out.println("r_num:" + r_num);
        
        // ------------------------ loop -> 10회
        boolean b = false;
        int count = 0;
        
        while(count < 10) {
        
            // 3.유저의 입력
            while(true) {    
                b = false;
                System.out.print((count + 1+ "회 숫자입력 = ");
            //    u_num = sc.nextInt();
                s_num = sc.next();        
                // System.out.println( s_num.length() );
                // abc
                for(int i = 0;i < s_num.length(); i++) {
                    char c = s_num.charAt(i);
                //    System.out.println("c = "+ c);
                    
                    /*
                    if(c != '0' && c != '1' && c != '2' &&
                        c != '3' && c != '4' && c != '5' &&
                        c != '6' && c != '7' && c != '8' &&
                        c != '9') {
                        System.out.println("정상적인 숫자를 입력해 주십시오");
                        b = true;
                        break;
                    }
                    */
                    
                    int asc = (int)c;
                    if(asc < 48 || asc > 57) { // 숫자의 범위
                        System.out.println("정상적인 숫자를 입력해 주십시오");
                        b = true;
                        break;
                    }                    
                }            
                if(b == false) {    // 문제 없음
                    //System.out.println("문제없이 입력함");
                    break;
                }    
                
                System.out.println("b = " + b);
            }
            
            // 문자열 -> 숫자
            u_num = Integer.parseInt( s_num );
                    
            // 4.판정(크다/작다)
            if(u_num > r_num) {
                System.out.println("너무 큽니다");
            }
            else if(u_num < r_num) {
                System.out.println("너무 작습니다");
            }
            else {    // 숫자를 맞춘경우
                clear = true;
                break;
            }
            
            count++;
        }
        // ------------------------
        
        // 5.결과
        if(clear) {
            System.out.println("Game Clear!");            
        }
        else {
            System.out.println("Game Over~");
        }
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
// continue : 생략(skip)
/* 
      Loop문과 같이 사용
      
      
      while(){
          처리1
          처리2
          처리3
          if(조건식){
              continue;
          }
          처리4
          
          연산식
          }
 */
        
        //예제1)
        
        for(int i=0 ; i<10; i++) {
            System.out.println("for start");
            System.out.println("i = "+i);
            
            if (i>4) {
                continue;
            }
            System.out.println("for end"); // i=5일때는 for end가 출력되지 않는다
        }
        
        
        //예제2)
        
        int w=0;
        while (w<10) {
            System.out.println("while start");
            System.out.println("w = "+w);
            w++;     // 여기에 써야 무한루프X
            if(w>4) {
                continue;
            }
            System.out.println("while end");
             // w++; // 여기있으면 연산자가 아래있어서 무한루프에 걸리게 된다.
        }
    
cs

'JAVA > basic' 카테고리의 다른 글

파이널 / 상수 / final / 변수 고정  (0) 2020.09.28
실수 연산의 오류  (0) 2020.09.28
자료형의 종류와 구분 / 변수 / 변수의 기본값과 초기화  (0) 2020.09.28
Break 문  (0) 2020.09.24
Loop 문 ( Do while 문 )  (0) 2020.09.24
Loop문 ( while 문 )  (0) 2020.09.24
Loop문 ( foreach 문 ) / 배열과 foreach문  (0) 2020.09.23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 
        
    //이중for문 탈출예제
        
        for(int i=0; i<10; i++) {
            
            System.out.println("i = "+i);
        
            for(int j =0; j<5 ; j++){
                System.out.println("\tj = " + j);
                if(i==5 && j==3) {
                    break;  
                    //for문 1개에 break 1개 i=5 j=3에 탈출하지만 다시 i=6부터 돌아간다.
                }
            }
        }
            
    // 이중for문 탈출 방법 1
            
            boolean b = false;
            
            for(int i=0; i<10; i++) {
                
                System.out.println("i = "+i);
            
                for(int j =0; j<5 ; j++){
                    System.out.println("\tj = " + j);
                    if(i==5 && j==3) {
                        b = true;
                    }
                    if(b == true) {
                        break;
                    }
                    
                }
                if(b == true) {
                    break;
    }
 
    }
            
            
        //  이중for문 탈출 방법 2
            
            outside:for(int i=0; i<10; i++) {   // outside를 앞에도 적어줌 ~:
                
                System.out.println("i = "+i);
            
                for(int j =0; j<5 ; j++){
                    System.out.println("\tj = " + j);
                    if(i==3 && j==2) {
                        break outside; // 단어는 outside가 아니라 아무거나 가능  
                        
                    }
                }
            }
            
            
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
/* 
 
     break문 : loop 탈출
     
 */
        
        for (int i=0; i<100; i++) {
            System.out.println("i = " + i);
            if(i==23) {
                break;
                
            }
        }
cs

 

Loop 문 이름으로 Break

1
2
3
4
5
        outerLoop : for(i=1; i<10; i++) {
            for(int j=1; j<10 ; j++) {
                System.out.println("["+i+","+j+"]");
                if(i%2==0 && j%2==0) {
                    break outerLoop;
cs

'JAVA > basic' 카테고리의 다른 글

실수 연산의 오류  (0) 2020.09.28
자료형의 종류와 구분 / 변수 / 변수의 기본값과 초기화  (0) 2020.09.28
Continue 문 / 무한루프  (0) 2020.09.24
Loop 문 ( Do while 문 )  (0) 2020.09.24
Loop문 ( while 문 )  (0) 2020.09.24
Loop문 ( foreach 문 ) / 배열과 foreach문  (0) 2020.09.23
Loop문 ( for문 )  (0) 2020.09.23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    /*
     
         do while문
         -사용빈도가 떨어진다.
         -조건이 맞지 않아도 1번은 처리하기 때문 (조건이 밑에 있으므로)
         
         do{
             처리
             연산식         
         }while(조건식);
     
     */
            
//    예제1)
 
    int dw=0;
    
    do {
        System.out.println("dw = "+ dw);
        dw++;
    }while(dw<10);
    
// 예제2)
    
    int dw=10//10으로수정했음
    
    do {
        System.out.println("dw = "+ dw);
        dw++;
    }while(dw<10); // 조건과 틀렸는데도 한번 실행됨
cs

'JAVA > basic' 카테고리의 다른 글

자료형의 종류와 구분 / 변수 / 변수의 기본값과 초기화  (0) 2020.09.28
Continue 문 / 무한루프  (0) 2020.09.24
Break 문  (0) 2020.09.24
Loop문 ( while 문 )  (0) 2020.09.24
Loop문 ( foreach 문 ) / 배열과 foreach문  (0) 2020.09.23
Loop문 ( for문 )  (0) 2020.09.23
스위치 문 ( Switch 문 )  (0) 2020.09.23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    // 10명의 데이터를 입력받도록 한다.
    
    Scanner sc = new Scanner(System.in);
    
    
    // for문으로 풀기
    int arrNum[] = new int[10];
    
    for (int i =0; i<10 ; i++) {
        System.out.println((i+1)+"번째 데이터 = ");
        arrNum[i] = sc.nextInt();
    }
    
    
    // while문으로 풀기
    int numArr[] = new int[10];
    int w1;
    w1=0;
    
    while (w1<10) {
        System.out.println((w1+1+"번째 데이터= ");
        numArr[w1]=sc.nextInt();
        
        w1++;
    }
            
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
        - while문
        
        변수의 선언
        초기화
        
        while(조건식){
            처리
            연산식
        }
        
*/
        
        int w;
        w=0;
        
        while(w<10) {
            System.out.println("w= "+w);
            
            w++;
        }
cs

 

while(ture) = 무한루프

'JAVA > basic' 카테고리의 다른 글

Continue 문 / 무한루프  (0) 2020.09.24
Break 문  (0) 2020.09.24
Loop 문 ( Do while 문 )  (0) 2020.09.24
Loop문 ( foreach 문 ) / 배열과 foreach문  (0) 2020.09.23
Loop문 ( for문 )  (0) 2020.09.23
스위치 문 ( Switch 문 )  (0) 2020.09.23
조건문 ( if, if-else, else if )  (0) 2020.09.23

1
2
3
4
5
6
7
8
9
10
11
12
13
            int n=0;
            
            for(int j=00 ; j<9 ; j++) {
                
                if(j<5
                    n++;
                else n--;
                
                for (int i =0; i<n; i++) {
                    System.out.print("*");
                }
                System.out.println();
            }
cs

참고 링크  : pridiot.tistory.com/24

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        char arr[] = {'D''W''O''A''B'};
        // 모두 소문자로 변경한 후 출력하라.
        // A=65, a=97 -> 32 
        // System.out.println("ASCII = " + (int)arr[0]);  알파벳을 ASCII 코드로변경해서 출력해보기
        
        int asc = (int)arr[0];    // D의 ASCII코드값 얻기
        asc = asc+32;            // 소문자로 변환        
        char ch = (char)asc;    // 문자로변환
        System.out.println(ch); // 출력
        
        for(int i=0 ; i<arr.length ; i++) {
            char c = arr[i]; // char c에 순서대로 알파벳 넣기
            int asc2 = (int)c; // arr[i]값의 ASCII 코드값 얻기
            asc2 = asc2+32// 소문자로 변환하기
            arr[i] = (char) asc2; // 다시 arr[i]에 소문자로 바뀐 값 넣어주기
            System.out.print(arr[i]);
            
        }
cs

+ Recent posts