java如何从公共静态void main(String[]args)传递值
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();
}
}
}
# 1 楼答案
Old answer.
您正在重新创建
Attacker
类的实例,问题是low
值仅与该特定实例关联。因此,当您在Defender
中的事实之后创建一个新的时,您将失去该值。我还将Attacker
和Defender
完全分离,并创建了一个Battle
类,该类将以Attacker
和Defender
为例,决定哪一个获胜。那个类是实例的对峙。这提高了可读性、逻辑性等。这是一个更好的设计。你的情况有点混乱,没有太多结构(无意冒犯——只是学习):主类:
攻击者类别:
防守者职业:
最后,战斗类: