본문 바로가기

프로그램 만들기

Java) 학생 학번 확인 프로그램

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
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Student st1 = new Student("손오공""1111");
        Student st2 = new Student("저팔계""2222");
        Student st3 = new Student("사오정""3333");
        
        ArrayList <Student>list = new ArrayList<Student>();
        list.add(st1);
        list.add(st2);
        list.add(st3);
        
        Scanner sc = new Scanner(System.in);
        
        while(true) {
            System.out.println("계속 검색하고 싶으시면  y, 종료하고 싶으면 n");
            
            String input = sc.next();
            if(input.equals("y")) {
                System.out.println("검색을 시작합니다.");
                String name = sc.next();
                boolean flag = false;
                for(Student stu : list) {
                    if(stu.getName().equals(name)) {
                        System.out.println("해당하는 학생의 학번은 "+stu.getNo());
                        flag = true;
                    }
                }
                if(flag == false)
                    System.out.println("그 학생은 없습니다.");
                
            }
            else if(input.equals("n")) {
                break;
            }
        }
        System.out.println("프로그램이 종료되었습니다.");
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">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
 
public class Student {
    private String name;
    private String no;
    
    public Student(String name, String no) {
        super();
        this.name = name;
        this.no = no;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getNo() {
        return no;
    }
    public void setNo(String no) {
        this.no = no;
    }
    
    
    
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

이러한 프로그램을 만들면서 프로그램의 구동 원리와 기본적인 개념, 코드 구현력이 올라갔다.