有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    Old answer.


    您正在重新创建Attacker类的实例,问题是low值仅与该特定实例关联。因此,当您在Defender中的事实之后创建一个新的时,您将失去该值。我还将AttackerDefender完全分离,并创建了一个Battle类,该类将以AttackerDefender为例,决定哪一个获胜。那个类是实例的对峙。这提高了可读性、逻辑性等。这是一个更好的设计。你的情况有点混乱,没有太多结构(无意冒犯——只是学习):

    主类:

    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
            }
        }
    
    }