要求

code

原來的code

Main.java

import java.util.*;

class Beginner{
	private int STR=1,DEX=1,INT=1,HP=50,MP=50,LV=1;
	void use_skill(Profession p) {
		System.out.println(p.skill());
	}
	void showAP(Profession p) {
		System.out.println("----"+p.getProfession()+" LV."+LV+"----");
		System.out.println("HP : "+HP);
		System.out.println("MP : "+MP);
		System.out.println("STR : "+STR);
		System.out.println("DEX : "+DEX);
		System.out.println("INT : "+INT);
	}
	void levelup(Profession p) {		
		LV++;
		System.out.println("Level Up! LV."+LV+" Congratulations !");
		HP+=p.upgradeHP();
		MP+=p.upgradeMP();
		STR+=p.upgradeSTR();
		DEX+=p.upgradeDEX();
		INT+=p.upgradeINT();
	}
}

public class Main{
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		
		System.out.println("Hello Beginner ! Please choose your Profession.");
		System.out.println("(1) Warrior");
		System.out.println("(2) Magician");
		System.out.println("(3) Archer");
		int choice=sc.nextInt();
		
		Beginner person = new Beginner();
		Profession profession=null;
		
		switch(choice) {
			case 1:
				profession = new Warrior();
				break;
			case 2:
				profession = new Magician();
				break;
			case 3:
				profession = new Archer();
				break;
			default:
				break;
		}
		
		while(choice>=1&&choice<=3){
			System.out.println("\n\n\nWhat do you want to do?");
			System.out.println("(1) Level Up!");
			System.out.println("(2) Use skill.");
			System.out.println("(3) Show my ability point.");
			System.out.println("(4) Exit.");
			int select=sc.nextInt();
			
			if(select==4)
				break;
			
			switch(select) {
				case 1:
					person.levelup(profession);
					break;
				case 2:
					person.use_skill(profession);
					break;
				case 3:
					person.showAP(profession);
					break;
				default:
					break;
			}
			
			
		}
	}
	
}


自己寫的code

Profession.java

Profession 是一個 interface


public interface Profession {
	String skill(); //return the Profession’s skill 
	String getProfession(); //return the profession name.
	int upgradeHP(); //return HP (health point) value when level up.
	int upgradeMP(); //return MP (magic point) value when level up.
	int upgradeSTR(); //return STR (strength) value when level up.
	int upgradeDEX(); //return DEX (dexterity) value when level up.
	int upgradeINT(); //return INT (intelligence) value when level up.
}

Warrior.java


public class Warrior implements Profession {
	String skill_name = "Glacial Hammer !!!", profession_name = "Warrior";
	int _hp = 100, _mp = 30, _str = 5, _dex = 3, _int = 1;
	public String skill() {
		return skill_name;
	}
	public String getProfession() {
		return profession_name;
	}
	public int upgradeHP() {
		return _hp;
	}
	public int upgradeMP() {
		return _mp;
	}
	public int upgradeSTR() {
		return _str;
	}
	public int upgradeDEX() {
		return _dex;
	}
	public int upgradeINT() {
		return _int;
	}
}

Magician.java


public class Magician implements Profession{
	String skill_name = "Firestorm !!!", profession_name = "Magician";
	int _hp = 30, _mp = 100, _str = 1, _dex = 3, _int = 5;
	public String skill() {
		return skill_name;
	}
	public String getProfession() {
		return profession_name;
	}
	public int upgradeHP() {
		return _hp;
	}
	public int upgradeMP() {
		return _mp;
	}
	public int upgradeSTR() {
		return _str;
	}
	public int upgradeDEX() {
		return _dex;
	}
	public int upgradeINT() {
		return _int;
	}
}

Archer.java


public class Archer implements Profession{
	String skill_name = "Rain of Arrows !!!", profession_name = "Archer";
	int _hp = 80, _mp = 50, _str = 3, _dex = 5, _int = 1;
	public String skill() {
		return skill_name;
	}
	public String getProfession() {
		return profession_name;
	}
	public int upgradeHP() {
		return _hp;
	}
	public int upgradeMP() {
		return _mp;
	}
	public int upgradeSTR() {
		return _str;
	}
	public int upgradeDEX() {
		return _dex;
	}
	public int upgradeINT() {
		return _int;
	}
}

解題思路

interface