目录
1.用随机数表示概率
public static boolean eighty_five() {
int n = random.nextInt(100);
boolean m;
if(n < 85){
m = true;
return m;
}else{
m = false;
return m;
}
}
public static boolean fifity() {
int n = random.nextInt(100);
boolean m;
if(n < 50){
m = true;
return m;
}else{
m = false;
return m;
}
}
2.用arraylist中的某一属性作为筛选标准-从大到小
//Sort by hero speed
public static List<Hero> SpeedByHero(List<Hero> list) {
Collections.sort(list, new Comparator<Hero>() {
@Override
public int compare(Hero o1, Hero o2) {
int flag;
flag = o2.getSpeed()-o1.getSpeed();
return flag;
}
});
return list;
}
3.判断字符是否为空与不空工具类
//Checks if the character is empty
public class StringUtil {
public static boolean isEmpty(String str) {
if(str==null || "".equals(str.trim())) {
return true;
}else {
return false;
}
}
public static boolean isNotEmpty(String str) {
if(str!=null && !"".equals(str.trim())) {
return true;
}else {
return false;
}
}
//判断string是否为数字
public static boolean isNumeric(String str){
for (int i = 0; i < str.length(); i++){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
}
4.写游戏任务-先写抽象类,然后再写实现类,比较正规
public abstract class Role {
private String name;
private int speed;
private int power;
private int life;//Maximum health
private int health;//Health point
//武器
private int type;//Weapon status: 0 for no weapon, 1 for fire, 2 for water, and 3 for ice
private int type_power;//The power of the weapon
//技能
private String sName1;//attack(%80 chance of success. )
private String sName2;//heal(%50 chance of success. heals teammates and yourself)
private String sName3;
//power up(75% probability of success, when the speed is greater than 1 available, each successful use, speed half, rounded up )
//attack method
public abstract void attack(int skill,Role role,Villain villain);
//skill method
public abstract String skill1(Villain villain);
public abstract String skill2(Role role);
public abstract String skill3(Role role);
public Role(String name, int power, int life, int speed,int type,
String sName1, String sName2, String sName3) {
super();
this.name = name;
this.power = power;
this.life = life;
this.speed = speed;
this.type = type;
this.sName1 = sName1;
this.sName2 = sName2;
this.sName3 = sName3;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getType_power() {
return type_power;
}
public void setType_power(int type_power) {
this.type_power = type_power;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public String getsName1() {
return sName1;
}
public void setsName1(String sName1) {
this.sName1 = sName1;
}
public String getsName2() {
return sName2;
}
public void setsName2(String sName2) {
this.sName2 = sName2;
}
public String getsName3() {
return sName3;
}
public void setsName3(String sName3) {
this.sName3 = sName3;
}
}
import demo.Role;
import demo.Villain;
import utils.RandomUtils;
public class Hero extends Role {
private boolean flag=false;
private int total=0;//Total healing or total damage
public Hero(String name, int power, int life,int speed, int type,
String sName1, String sName2, String sName3) {
super(name,power,life,speed,type, "Attack","Heal", "Power Up");
}
public void attack(int skill,Role role, Villain villain) {
role.setHealth(role.getHealth());
if(skill == 1) {
skill1(villain);
}else if(skill == 2) {
skill2(role);
}else {
skill3(role);
}
}
//Attack method(%80)
public String skill1(Villain villain) {
if(RandomUtils.eighty()) {
if(this.getType()!=0) {
if(this.getType()==1&&villain.getType().contains("3")||this.getType()==2&&villain.getType().contains("1")||this.getType()==3&&villain.getType().contains("2")) {
//Property restraint
this.setType_power(20);
flag=true;
}else {
//Don't resist
this.setType_power(10);
flag=false;
}
//Method implementation
total=this.getPower()+this.getType_power();
villain.setHealth(villain.getHealth()-total);
//Judge monster health
if(villain.getHealth()<=0) {
return "Hero "+this.getName()+" defeatd "+villain.getName();
}else {
if(flag) {
return "Hero "+this.getName()+" "+this.getsName1()+" "+villain.getName()+",causing "+total+"damage,The monster still has "+villain.getHealth()+" health point";
}else {
return "Hero "+this.getName()+" "+this.getsName1()+" "+villain.getName()+",causing "+total+" damage,The monster still has "+villain.getHealth()+" health point";
}
}
}else {
//No weapons
total=this.getPower();
villain.setHealth(villain.getHealth()-total);
//Judge monster health
if(villain.getHealth()<=0) {
return "Hero victory";
}else {
return "Hero "+this.getName()+" "+this.getsName1()+" "+villain.getName()+",causing"+total+" damage,The monster still has "+villain.getHealth()+" health point";
}
}
}else {
return "Against failure";
}
}
//heal(%50)
public String skill2(Role role) {
if(RandomUtils.fifity()) {
if(role.getHealth()+this.getPower()<=role.getLife()) {
total=this.getPower();
role.setHealth(role.getHealth()+total);
}else {
total=role.getLife()-role.getHealth();
role.setHealth(role.getLife());
}
return this.getName()+" heal "+role.getName()+",heal "+total+" health point";
}else {
return "treatment failure";
}
}
//power up (75%)
public String skill3(Role role) {
if(RandomUtils.eighty_five()) {
role.setPower(this.getPower()*2);
//round up to an integer
role.setSpeed(this.getSpeed()/2+ (this.getSpeed() % 2 != 0 ? 1 : 0));
return this.getName()+" power up "+role.getName()+",Twice as strong, half as fast";
}else {
return "Reinforcing failure";
}
}
}
5.JTextArea与JTextField
JTextField与JTextArea都是文本域,但是JTextField只有中文有自动换行功能,解决起来特别麻烦,我这里为了省事,直接换成了JTextArea,这个有专门的方法,可以英文等自动换行。
private JTextArea MsgField;
MsgField = new JTextArea();
MsgField.setLineWrap(true); //Enable line wrapping
MsgField.setWrapStyleWord(true); // Activate the line break function
6.List
list.size()返回的是list大小(从1开始)
list.get(index)得到对应索引的值(index从0开始)。
list.remove(index);删除索引对应的元素(index从0开始)
7.Jfram小知识
dispose();销毁当前页面
new MonsterFrame().setVisible(true);调用新页面并显示
jfram页面间传值有几种方法,这里我简要说明一下
参数直接传值:
new MonsterCombatOutcomes(msg).setVisible(true);
用静态变量传值
public static int temporary;
这两个是我经常用的
给按钮或文本框赋值
都有对应的setText()方法
获得对应的值有getText()方法
弹出提示
JOptionPane.showMessageDialog(null, "The name cannot be empty!");
jframe居中显示,放在Jpane建立后
setLocationRelativeTo(null);
8.Jtable获取选中值
model = new DefaultTableModel(new Object[][] {
},
new String[] {
header
});
table = new JTable(model);
scrollPane.setViewportView(table);
Object valueAt = model.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
//Add role data to the table
private void fillTable(List<Hero> list) {
DefaultTableModel dtm=(DefaultTableModel) table.getModel();
dtm.setRowCount(0);
for (Hero hero : list) {
Vector<String> v=new Vector<String>();
v.add(hero.getName());
dtm.addRow(v);
}
}