728x90
포켓몬 클래스는 전 글이랑 그대로 만들고
Main클래스는 아래 처럼 만들면 된다
Main클래스
import java.util.ArrayList;
import java.util.Scanner;
public class PockemonMain {
public static void main(String[] args) {
// 반보적으로 포켓몬을 생성하기 위함
Scanner sc = new Scanner(System.in);
ArrayList<Pockemon> pockeList = new ArrayList<Pockemon>();
while (true) {
System.out.println("이름, 타입,방어력, 공격력,체력 입력");
String name = sc.next(); // 이름
String type = sc.next(); // 타입
int defense = sc.nextInt(); // 방어력
int attack = sc.nextInt(); // 공격력
int hp = sc.nextInt(); // 체력
pockeList.add(mon);
for (int i = 0; i < pockeList.size(); i++) {
System.out.println("mon" + (i + 1) + "의 이름 : " + pockeList.get(i).getName());
System.out.println("mon" + (i + 1) + "의 타입 : " + pockeList.get(i).getType());
System.out.println("mon" + (i + 1) + "의 방어력 : " + pockeList.get(i).getDefense());
System.out.println("mon" + (i + 1) + "의 공격력 : " + pockeList.get(i).getAttack());
System.out.println("mon" + (i + 1) + "의 체력 : " + pockeList.get(i).getHp());
System.out.println();
}
} // while문이 끝나는 지점
}
}
포켓몬 클래스
package Pockemon;
public class Pockemon {
// 스스로 동작X 설계만 진행하는 클래스 --> main 메소드를 쓰지 않겠다
// 포켓몬 설계도 -> 포켓몬 정보 저장, 기능
// 포켓몬 정보 : 속성(타입) - String, 이름- String, 방어력 - int, 공격력 - int, 체력 - int
// 포켓몬 속성 정의
// private : 접근지정자 제한주기 -> 다른 사용자에 의해서 정보가 수정되지 않도록 하기 위함!
private String type;
private String name;
private int defense;
private int attack;
private int hp;
// 생성자 메소드
public Pockemon(String name, String type, int defense, int attack, int hp) {
this.type = type;
this.name = name;
this.defense = defense;
this.attack = attack;
this.hp =hp;
}
// 메소드 get : 속성을 조회
public String getType() {
return type;
}
public String getName() {
return name;
}
public int getDefense() {
return defense;
}
public int getAttack() {
return attack;
}
public int getHp() {
return hp;
}
// 메소드 set : 속성을 변경
public void setType(String type) {
this.type = type;
}
public void setName(String name) {
this.name = name;
}
public void setDefense(int defense) {
this.defense = defense;
}
public void setAttack(int attack) {
this.attack = attack;
}
public void setHp(int hp) {
this.hp = hp;
}
}
728x90
'자바' 카테고리의 다른 글
[자바]객체 실습 - 포켓몬 (0) | 2021.04.22 |
---|---|
[자바]객체 정의 (붕어빵) (0) | 2021.04.22 |
[자바]배열 Collection (0) | 2021.04.20 |
[자바]메소드(Method)란? (0) | 2021.04.16 |
[자바]2차원 배열 (0) | 2021.04.16 |