public static void main(String[] args) {
Scanner data = new Scanner(System.in);
double low = data.nextDouble();
Attacker type = new Attacker();
type.setLow(low);
Defender fight = new Defender();
fight.result();
}
后卫班
private int ATKvalue;
public void result() {
Attacker xxx = new Attacker();
ATKvalue = xxx.ATKtype();
}
攻击者类
public Attacker() {
double low = 0
}
public void setLow(double lowpercent) {
low = lowpercent;
}
public int ATKtype() {
System.out.println(low);
}
我简化了我的代码,但这是一样的想法。当我运行它时,low等于0而不是用户输入。如何将其更改为等于用户输入?
我的代码:
import java.util.Random;
public class Attacker {
public double low, med, genPercent;
private int lowtype, medtype, hightype;
public Attacker() {
low = 0;
lowtype = 0;
medtype = 1;
hightype = 2;
}
public void setLow(double low) {
this.low = low;
}
public double getLow(){
return(low);
}
public void setMed(double med) {
this.med = med;
}
public int ATKtype() {
System.out.println(low);
Random generator = new Random();
genPercent = generator.nextDouble() * 100.0;
System.out.println(genPercent);
System.out.println(low);
if ( genPercent <= low ) {
System.out.println("low");
return (lowtype);
}
else if ( genPercent <= med + low ) {
System.out.println("med");
return (medtype);
}
else {
System.out.println("high");
return (hightype);
}
}
}
import java.util.Random;
public class Defender {
private int lowtype, medtype, hightype, DEFvalue, ATKvalue;
private double genPercent;
public Defender() {
lowtype = 0;
medtype = 1;
hightype = 2;
}
public int getDEFtype() {
Random generator = new Random();
genPercent = generator.nextDouble() ;
if ( genPercent <= 1d/3d ) {
return (lowtype);
}
else if ( genPercent <= 2d/3d ) {
return (medtype);
}
else {
return (hightype);
}
}
public void result() {
Manager ATK = new Manager();
Defender DEF = new Defender();
DEFvalue = DEF.getDEFtype();
ATKvalue = ATK.getATKtype();
System.out.println(DEFvalue);
System.out.println(ATKvalue);
if ( ATKvalue == DEFvalue ) {
System.out.println("block");
}
else {
System.out.println("hit");
}
}
}
import java.util.Scanner;
public class Manager {
public int getATKtype() {
Attacker genType = new Attacker();
int attack = genType.ATKtype();
return (attack);
}
public static void main(String[] args) {
System.out.print("Number of attack rounds: " );
Scanner data = new Scanner(System.in);
int round = data.nextInt();
System.out.println("Enter percentages for the number of attacks that will be "
+ "directed: low, medium, high. The total of the three percentages "
+ "must sum to 100%");
System.out.print("Percentage of attacks that will be aimed low: ");
double low = data.nextDouble();
System.out.print("Percentage of attacks that will be aimed at medium height: ");
double med = data.nextDouble();
System.out.print("Percentage of attacks that will be aimed high: ");
double high = data.nextDouble();
if ( low + med + high != 100 ){
System.out.println("The sum is not 100%. Equal probablilty will be used.");
low = med = high = 100d/3d ;
}
Attacker type = new Attacker();
type.setLow(low);
type.setMed(med);
System.out.print(type.getLow());
for ( int i = 0 ; i < round ; i++) {
Defender fight = new Defender(Attacker type);
fight.result();
}
}
}
答案 0 :(得分:4)
您正在重新创建Attacker
类的实例,问题在于low
值仅与该特定实例相关联。因此,当您在Defender
中创建新事件后,您将失去该值。我还完全将Attacker
和Defender
分开,并创建了一个Battle
类,该类将采用Attacker
和Defender
的实例并决定哪一个获胜。那个阶级是面对实例的。这提高了可读性,逻辑性等。它是一种更好的设计。没有多少结构(没有冒犯 - 只是学习),你的有点混乱:
主要课程:
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter attack low: ");
double low = input.nextDouble();
Attacker attacker = new Attacker();
attacker.setLow(low);
Defender defender = new Defender();
Battle battle = new Battle(attacker, defender);
battle.result();
}
攻击者类:
import java.util.Random;
public class Attacker {
public double low = 0;
public void setLow(double low) {
this.low = low;
}
public double getATKtype() {
double genPercent = new Random().nextDouble() * 100.0;
System.out.println(low);
if ( genPercent <= low ) {
System.out.println("Attack: low");
return 0; // low type
}
else if ( genPercent <= 1 + low ) { // genPercent <= medium + low
System.out.println("Attack: medium");
return 1; // medium type
}
else {
System.out.println("Attack: high");
return 2; // high type
}
}
}
后卫类:
import java.util.Random;
public class Defender {
public double getDEFtype() {
double genPercent = new Random().nextDouble();
if ( genPercent <= 1d/3d ) {
System.out.println("Defense: low");
return 0; // low type
}
else if ( genPercent <= 2d/3d ) {
System.out.println("Defense: medium");
return 1; // medium type
}
else {
System.out.println("Defense: high");
return 2; // high type
}
}
}
最后,战斗类:
public class Battle {
private double attackValue = 0;
private double defenseValue = 0;
public Battle (Attacker attacker, Defender defender) {
attackValue = attacker.getATKtype();
defenseValue = defender.getDEFtype();
}
public void result() {
if (attackValue == defenseValue) {
System.out.println("Block");
} else if (attackValue > defenseValue) {
System.out.println("Hit");
} else { // attack is lower than defense
// do what you need for that
}
}
}
答案 1 :(得分:2)
要将值传递给另一个类,您需要将其作为另一个类的构造函数或setter方法的参数传递。
因此,如果您为攻击者提供setLow(int low)
方法,则可以传递信息。
public void setLow(int low) {
this.low = low;
}
然后在需要时调用该方法。