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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 
package com.test;
 
import java.util.Random;
import java.util.Scanner;
 
public class Class {
    
    public static void main(String[] args) {
 
        
        //m1();
        m2();     
             
         //추가] 학생 100명으로 늘리기
             
    }
 
    
    
    private static void m2() {
        
        //추가] 학생 100명으로 늘리기 -> 난수 발생
        Scanner scan = new Scanner(System.in);
        Member[] members = new Member[100];
        Random rnd = new Random();
        
        //이름
        String[] name1 = {"김""이""박""최""정"};
        String[] name2 = {"민""효""세""혁""동""길""준""하""호""현"};
        //주소
        String[] address1 = {"서울시""인천시""부산시""광주시""제주시"};
        String[] address2 = {"강남구""강서구""강동구""강북구""동대문구"};
        String[] address3 = {"역심동""도곡동""길동""연희동""제기동"};
        //전화번호
        String[] tel = {"010""019""017""011"};
        
        
        for (int i=0; i<members.length; i++) {
            members[i] = new Member();
            
            //학생 정보 입력 -> 난수 처리
            members[i].name = name1[rnd.nextInt(name1.length)] 
                            + name2[rnd.nextInt(name2.length)] 
                            + name2[rnd.nextInt(name2.length)];             
            
            members[i].age = rnd.nextInt(41+ 20//나이:20~60
            
            members[i].address = address1[rnd.nextInt(address1.length)]
                                 + address2[rnd.nextInt(address2.length)]
                                 + address3[rnd.nextInt(address3.length)];
            members[i].tel = tel[rnd.nextInt(tel.length)] + "-" + rnd.nextInt(10)
                    + rnd.nextInt(10+ rnd.nextInt(10+ rnd.nextInt(10+ "-" + rnd.nextInt(10)
                    + rnd.nextInt(10+ rnd.nextInt(10+ rnd.nextInt(10);
        }
        
        //확인용
        for (int i=0; i<members.length; i++) {
            members[i].hello();
        }
        
        
    }
 
    
    
    private static void m1() {
        //3. 객체 만들기 (인스턴스)
        Member m1 = new Member();
        m1.name = "홍길동";
        m1.age = 20;
        m1.address = "서울시 영등포구 문래동";
        m1.tel = "010-2578-5413";
        
        //4. 학생 3명 -> 사용자입력 (배열)
        Scanner scan = new Scanner(System.in);
        Member[] members = new Member[3]; //방3개 만들기
                
            //루프돌리기
             for (int i=0; i<members.length; i++) {
                 members[i] = new Member(); //멤버가 생성됨
                 //members[i].name = "홍길동"; //아직 방만 있는 상태
                 
                 System.out.print("이름 : ");
                 members[i].name = scan.nextLine();
                 
                 System.out.print("나이 : ");
                 members[i].age = scan.nextInt();
                 scan.nextLine();
                 
                 System.out.print("주소 : ");
                 members[i].address = scan.nextLine();
                 
                 System.out.print("전화번호 : ");
                 members[i].tel = scan.nextLine();
            }
             
             //확인용
             for (int i=0; i<members.length; i++) {
                 members[i].hello();
             }
    }
 
    
}
 
//자바파일에 클래스를 2개 이상 선언한다면.,
//    - public 키워드를 가지는 클래스는 유일해야 한다.(대표 클래스)
//    - public 키워드르를 가진 클래스가 파일명이 된다.
class Member {
    
    //ex] 회원정보관리 프로그램
    //1.
    public String name;        //회원명    
    public int age;        //나이
    public String address;    //주소
    public String tel;        //전화번호
    
    //2.
    public void hello() {
        System.out.printf("저는 %s입니다. 나이는 %d살이고 사는 곳은 %s입니다. 전화번호는 %s입니다.\n"
                            ,name
                            ,age
                            ,address
                            ,tel);
    }
}
cs


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

STATIC(정적)1  (0) 2015.05.22
ACCESS(접근지정자)  (0) 2015.05.22
PACKAGE  (0) 2015.05.22
RANDOM(난수생성)  (0) 2015.05.22
ARAARY(배열)  (0) 2015.05.22

+ Recent posts