negno
개발Log
negno
전체 방문자
오늘
어제
  • 분류 전체보기
    • Project
      • Mini_Project
      • PTSD_Project
    • Algorithm
      • Elice
      • JavaFestival
    • BACK-END
      • C Programming
      • JAVA
      • JSP Servlet
      • Python
      • Spring
      • Machine Learning
    • FRONT-END
      • HTML CSS
      • JavaScript
    • Application
      • Android
    • DataBase
      • Oracle
      • MySql
    • IoT
      • Arduino
      • Raspberry pi

티스토리

hELLO · Designed By 정상우.
negno

개발Log

Java 객체 배열 예제
BACK-END/JAVA

Java 객체 배열 예제

2022. 7. 19. 09:41

EX_01. 포켓몬 Game

1-1.Pokemon 설계

package Pokemon;

public class Pokemon {

	// 포켓몬 설계도
	// 1. 필드(속성, 데이터, 변수)
	private String name;
	private String type;
	private String skill;
	private int atk;
	private int hp;
	
	
	public Pokemon(String name, String type, String skill, 
			int atk, int hP) {
		this.name = name;
		this.type = type;
		this.skill = skill;
		this.atk = atk;
		this.hp = hP;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSkill() {
		return skill;
	}

	public void setSkill(String skill) {
		this.skill = skill;
	}

	public int getAtk() {
		return atk;
	}

	public void setAtk(int atk) {
		this.atk = atk;
	}

	public int getHP() {
		return hp;
	}

	public void setHP(int hP) {
		this.hp = hP;
	}
	 public void hpshow() {
	      System.out.println(getHP());
	 }

	// 2. 메소드(객체의 행위, 기능)
	
}

 

1-2.Main

package Pokemon;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int i = 0;
		//설계도를 사용할 공간
		Pokemon pika = new Pokemon("피카츄", "전기", "백만볼트", 30, 200);
//		pika.setName("피카츄");
//		pika.setType("전기");
//		pika.setSkill("백만볼트 ~ !");
//		pika.setAtk(30);
//		pika.setHP(200);
		Pokemon kkobuk = new Pokemon("꼬부기","물","물대포",35,300);
		
			System.out.println("[1] 피카츄 [2] 파이리");
	         int select1 = sc.nextInt();
	         if(select1 == 1) {
	        	 i = 1;
	         }else {
	        	 i = 2;
	         }
	      while (true) {
	    	  System.out.println("[1] 일반 공격 [2] 스킬 공격");
	    	  int select2 = sc.nextInt();
	    	
	    	  if (pika.getHP() <= 0 || kkobuk.getHP() <= 0) {
	            break;
	         }
	         if (i == 1) {
	            if (select2 == 1) {
	               System.out.println("피카츄 공격");
	               kkobuk.setHP(kkobuk.getHP()-pika.getAtk());
	               System.out.println("꼬부기의 남은 체력 : " + kkobuk.getHP());
	               i++;
	               continue;
	            }
	            if (select2 == 2) {
	               System.out.println(pika.getSkill());
	               kkobuk.setHP(kkobuk.getHP()-pika.getAtk()*2);
	               System.out.println("꼬부기의 남은 체력 : " + kkobuk.getHP());
	               i++;
	               continue;
	            }
	         }

	         if (i == 2) {
	            if (select2 == 1) {
	               System.out.println("꼬부기 공격");
	               pika.setHP(pika.getHP()-kkobuk.getAtk());
	               System.out.println("피카츄의 남은 체력 : " + pika.getHP());
	               i--;
	               continue;
	            }
	            if (select2 == 2) {
	               System.out.println(kkobuk.getSkill());
	               pika.setHP(pika.getHP()-kkobuk.getAtk()*2);
	               System.out.println("피카츄의 남은 체력 : " + pika.getHP());
	               i--;
	               continue;
	            }
	         }
             
	         System.out.println("=====================");
          }
	      
	      if (pika.getHP() <= 0) {
	         System.out.println("꼬부기가 승리했다!!");
	      }
	      if (kkobuk.getHP() <= 0) {
	         System.out.println("피카츄가 승리했다!!");
	      }
	   }
	}

'BACK-END > JAVA' 카테고리의 다른 글

Java 상속 예제  (0) 2022.07.19
Java ArrayList 예제  (0) 2022.07.19
JAVA OOP 예제  (0) 2022.07.13
JAVA 메소드 예제  (0) 2022.07.13
JAVA 2차원배열 예제  (0) 2022.07.13
    negno
    negno

    티스토리툴바