我試圖比較兩個對象的實例在java中比較兩個實例變量
a。創建一個名爲Cyclops的超級英雄
Superhero Cyclops = new Superhero("Cyclops");
b。創建一個名爲巨像的超級英雄。龐然大物具有40
Superhero Colossus = new SuperHero("Colossus", 40);
稱爲水蚤的對象的第一個實例具有10默認值的強度,和所述第二對象已分配的40的值,這是代碼內的強度值。
我該如何制定一個戰鬥方法,考慮到他們的優勢,然後確定贏家?
的超級英雄類的代碼顯示如下
public class Superhero{
//private instance variables declared below
//name of the super hero
private String name;
//name of the strength variable
private int strength;
//The variable for the powerUp method
private int powerUp;
//variable to store the power of the power up
private int storePower;
//getter and setter methods.
public int getStrength(){
return strength;
}
/*
This method takes in an integer paremeter and based on that it does
some calculations.
There are four possible integer values you can pass in: 100, 75, 50 and 25.
If 100 has been passed in the storePower variable is set to 40 which would be
a power-up of 40, if 75 then power-up of 30, if 50 then a power-up of 20,
if 25 then a power-up of 10 and if the right ammount is not specified a power-up
of 0 will be assigned.
*/
public int powerUp(int powerUp){
this.powerUp = powerUp;
if(powerUp == 100){
storePower = 40;
}else if(powerUp == 75){
storePower = 30;
}else if(powerUp == 50){
storePower = 20;
}else if(powerUp == 25){
storePower = 10;
}else{
storePower = 0;
}
strength = strength + storePower;
System.out.println("After receiving a power-up: " + name + " has a Strength of: " + strength);
return strength;
}
public void fight(){
}
//constructor for if the player wanted to specify the name and the strength
public Superhero(String name, int strength){
this.name = name;
this.strength = strength;
System.out.println(name + " " + strength);
}
//if the user doesn't enter the strength as it is optional
//this constructor below will run and set the default
public Superhero(String name){
this.name = name;
strength = 10;
System.out.println(name + " " + strength);
}
}
,正如你可以看到公共無效的戰鬥,是空的,因爲這是我想創建方法。
也顯示下方
public class Fight{
public static void main(String[] args){
// a. Create a Superhero named Cyclops
Superhero Cyclops = new Superhero("Cyclops");
//b. Create a Superhero named Colossus. Colossus has a strength of 40
Superhero Colossus = new SuperHero("Colossus", 40);
Cyclops.fight(Cyclops.getStrength(), Colossus.getStrength());
//d. Give Cyclops a powerUp of 100
Cyclops.powerUp(100);
}
}
事實上,你在加強戰鬥後的獨眼巨人在任何重要的戰鬥? – ThisClark
[爲Java遊戲生成攻擊]可能的副本(http://stackoverflow.com/questions/29182412/generating-attack-for-java-game)。 –
是的,加電是爲獨眼巨人增加額外的力量加值,所以如果用戶輸入100加電,獨眼巨人將獲得40力量加值 – user5438578