【问题标题】:Java "attack" won't workJava“攻击”不起作用
【发布时间】:2016-12-16 08:55:11
【问题描述】:

使用给定的代码:

    import java.util.Scanner;
import java.util.Random;

public class Scenario1{
    public static void line(){
        System.out.println("----------");
    }


    public static int[] CharacterSheet(int HP, int MaxHP,int MinDMG, int MaxDMG){ // [0] = HP; [1] = MaxHP; [2] = MinDMG; [3] = MaxDMg;
        int[] machine;
        machine = new int[4];
        machine[0] = HP;
        machine[1] = MaxHP;
        machine[2] = MinDMG;
        machine[3] = MaxDMG;
        return machine;
    }

    public static int[] CreatureSheet(int HP, int MaxHP,int MaxDMG, int MinDMG){ // [0] = HP; [1] = MaxHP; [2] = MinDMG; [3] = MaxDMG;
        int[] machine;
        machine = new int[4];
        machine[0] = HP;
        machine[1] = MaxHP;
        machine[2] = MinDMG;
        machine[3] = MaxDMG;
        return machine;
    }

    public static String[] NPC(String race){ // [0] = race; [1] = surname; [2] = lastname;
        Random r = new Random();
        String surname = "";
        String lastname1 = "";
        String lastname2 = "";
        String lastname = "";
        if(race == "dwarf"){
            switch(r.nextInt(5)){
            case 0:
                surname = "Ugbor";
                break;
            case 1:
                surname = "Hegbor";
                break;
            case 2:
                surname = "Kragen";
                break;
            case 3:
                surname = "Ogar";
                break;
            case 4:
                surname = "Nguur";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname1 = "Iron";
                break;
            case 1:
                lastname1 = "Oak";
                break;
            case 2:
                lastname1 = "Steel";
                break;
            case 3:
                lastname1 = "Brave";
                break;
            case 4:
                lastname1 = "Coward";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname2 = "hammer";
                break;
            case 1:
                lastname2 = "heart";
                break;
            case 2:
                lastname2 = "sword";
                break;
            case 3:
                lastname2 = "shield";
                break;
            case 4:
                lastname2 = "might";
                break;
            }
            lastname = lastname1 + lastname2;
        }else if(race == "elf"){
            switch(r.nextInt(5)){
            case 0:
                surname = "Zevvin";
                break;
            case 1:
                surname = "Lukias";
                break;
            case 2:
                surname = "Biinlas";
                break;
            case 3:
                surname = "Lasthandir";
                break;
            case 4:
                surname = "Iskaal";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname = "Huulrash";
                break;
            case 1:
                lastname = "Bluurkas";
                break;
            case 2:
                lastname = "Hraataas";
                break;
            case 3:
                lastname = "Lektar";
                break;
            case 4:
                lastname = "Kuulskar";
                break;
            }
        }else{
            race = "dwarf";
            surname = "Ugbor";
            lastname = "Braveshield";
        }
        String[] code;
        code = new String[3];
        code[0] = race;
        code[1] = surname;
        code[2] = lastname;
        return code;
    }

    public static void attack(int[] atkd, int[] atkr){
        Random r = new Random();
        int rand = (r.nextInt(atkr[3] - atkr[2] + 1) + 1);
        atkd[0] -= rand;
    }

    public static void main(String[] args){
        int option2;
        String option;
        int turn = 0;
        turn = 0;
        int[] atkr;
        int[] atkd;
        String temp;
        int[] nobody;
        nobody = new int[1];
        nobody[0] = 0;
        int[] Char1Sheet = CharacterSheet(8, 8, 1, 4);
        int[] NPC1Sheet = CharacterSheet(15, 15, 1, 4);
        int[] Creature1Sheet = CreatureSheet(15, 15, 1, 2);
        Scanner sc = new Scanner(System.in);
        System.out.println("We kindly ask you to choose only what the program tells you to.");
        System.out.println("Press ENTER to proceed...");
        temp = sc.nextLine();
        System.out.println("Hello, player! Please tell me your character's name:");
        String Char1Name = sc.nextLine();
        System.out.println("Say hello to " + Char1Name + "! What village is it that he lives in?");
        String Village = sc.nextLine();
        String NPC1race = "";
        Random r = new Random();
        switch(r.nextInt(2)){
        case 0:
            NPC1race = "dwarf";
            break;
        case 1:
            NPC1race = "elf";
        }
        String[] NPC1 = NPC(NPC1race);
        System.out.println(Village + ", I see! So, " + Char1Name +" knows a fellow " + NPC1[0] + ", his name is " + NPC1[1] + " " + NPC1[2] + ". They know each other very well.");
        temp = sc.nextLine();
        System.out.println("Today, they both go out into the woods next to " + Village + ", in hopes of getting some wood.");
        temp = sc.nextLine();
        System.out.println("Night falls and " + Char1Name + " and " + NPC1[1] + " are still in the woods. Sadly, they got lost, so they start searching for a shelter.");
        temp = sc.nextLine();
        System.out.println("A grey creature comes out of nowhere and attacks " + Char1Name + "!");
        line();
        attack(Char1Sheet, Creature1Sheet);
        String Creature1Name = "GreyCreature";
        while((NPC1Sheet[0] > 0 && Creature1Sheet[0] > 0) || (Char1Sheet[0] > 0 && Creature1Sheet[0] > 0)){
            System.out.println("Stats:");
            System.out.println(Char1Name + ": " + Char1Sheet[0] + "/" + Char1Sheet[1] + "HP");
            System.out.println(NPC1[1] + ": " + NPC1Sheet[0] + "/" + NPC1Sheet[1] + "HP");
            System.out.println(Creature1Name + ": " + Creature1Sheet[0] + "/" + Creature1Sheet[1] + "HP");
            if(turn == 0){
                atkr = Char1Sheet;
                atkd = nobody;
                System.out.println("It is " + Char1Name + "'s turn. What does he do?");
                System.out.println("1.(a)ttack");
                option = sc.nextLine();
                if(option=="a"){
                    System.out.println(Char1Name + " attacks. Who should he attack?");
                    System.out.println("1.GreyCreature");
                    option2 = sc.nextInt();
                    if(option2 == 1){
                        atkd = Creature1Sheet;
                    }
                    attack(atkd, atkr);
                }
                turn = 1;
                temp = sc.nextLine();
            }else if(turn == 1){
                atkr = NPC1Sheet;
                atkd = nobody;
                System.out.println("It is " + NPC1[1] + "'s turn. What does he do?");
                System.out.println("1.(a)ttack");
                option = sc.nextLine();
                if(option=="a"){
                    System.out.println(NPC1[1] + " attacks. Who should he attack?");
                    System.out.println("1.GreyCreature");
                    option2 = sc.nextInt();
                    if(option2 == 1){
                        atkd = Creature1Sheet;
                    }
                    attack(atkd, atkr);
                }
                turn = 2;
                temp = sc.nextLine();
            }else if(turn == 2){
                atkr = Creature1Sheet;
                atkd=nobody;
                switch(r.nextInt(2)){
                case 0:
                    atkd = Char1Sheet;
                    break;
                case 1:
                    atkd = NPC1Sheet;
                }
                if(atkd == Char1Sheet){
                    System.out.println(Creature1Name + " attacks " + Char1Name + ".");
                }else if(atkd == NPC1Sheet){
                    System.out.println(Creature1Name + " attacks " + NPC1[1] + ".");
                }
                attack(atkd, atkr);
                turn = 0;
                temp = sc.nextLine();
            }
        }
        if(Char1Sheet[0] <= 0 && NPC1Sheet[0] <= 0){
            System.out.println("Sadly, the two friends did not survive the wildernees... This was the end of our heroes!");
        }else if(Creature1Sheet[0] <= 0){
            System.out.println("The two friends continue their search for shelter.");
        }
        System.out.println("Thank you so much for playing Virtual Sample Session 1 (VSS1) of my game, currently V.Alpha0.0.0!");
        System.out.print(temp);
        temp = sc.nextLine();
        sc.close();
    }
}

攻击方法不行!它说“绑定必须是积极的”。 ??? 它的工作方式是获取攻击者的 MaxDMG 和 MinDMG,然后使用它们在值之间给出一个随机数,然后从被攻击者的 HP 中减去该数字。

【问题讨论】:

  • 您需要使用equals()方法比较字符串。
  • 但它们不是字符串;他们是 int[]s。我不担心其余的代码,只是攻击方法不起作用!其余的都很好......
  • 线程“main”java.lang.IllegalArgumentException 中的异常:在 Scenario1.attack(Scenario1.java:139) 的 java.util.Random.nextInt(Unknown Source) 处绑定必须为正值 int rand = (r.nextInt(atkr[3] - atkr[2] + 1) + 1);在 Scenario1.main(Scenario1.java:183) 攻击(Char1Sheet, Creature1Sheet);
  • 异常信息很明确:您正在调用r.nextInt(int bound),其bound 值为0 或负数。因此atkr[2] 必须大于atkr[3]。我建议您在尝试调用 r.nextInt(..) 之前打印出 atkr[2]atkr[3] 的实际值

标签: java


【解决方案1】:

现在,我刚刚意识到我用 MaxDMG 切换了 MinDMG。对不起,我是白痴, .equals() 解决了我即将遇到的问题!谢谢!我对程序做了一些更改,现在我喜欢它!如果有人想玩它,我很高兴你这样做!再次感谢!

import java.util.Scanner;
import java.util.Random;

public class Scenario1{
    public static void line(){
        System.out.println("----------");
    }


    public static int[] CharacterSheet(int HP, int MaxHP,int MinDMG, int MaxDMG){ // [0] = HP; [1] = MaxHP; [2] = MinDMG; [3] = MaxDMg;
        int[] machine;
        machine = new int[4];
        machine[0] = HP;
        machine[1] = MaxHP;
        machine[2] = MinDMG;
        machine[3] = MaxDMG;
        return machine;
    }

    public static int[] CreatureSheet(int HP, int MaxHP,int MinDMG, int MaxDMG){ // [0] = HP; [1] = MaxHP; [2] = MinDMG; [3] = MaxDMG;
        int[] machine;
        machine = new int[4];
        machine[0] = HP;
        machine[1] = MaxHP;
        machine[2] = MinDMG;
        machine[3] = MaxDMG;
        return machine;
    }

    public static String[] NPC(String race){ // [0] = race; [1] = surname; [2] = lastname;
        Random r = new Random();
        String surname = "";
        String lastname1 = "";
        String lastname2 = "";
        String lastname = "";
        if(race == "dwarf"){
            switch(r.nextInt(5)){
            case 0:
                surname = "Ugbor";
                break;
            case 1:
                surname = "Hegbor";
                break;
            case 2:
                surname = "Kragen";
                break;
            case 3:
                surname = "Ogar";
                break;
            case 4:
                surname = "Nguur";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname1 = "Iron";
                break;
            case 1:
                lastname1 = "Oak";
                break;
            case 2:
                lastname1 = "Steel";
                break;
            case 3:
                lastname1 = "Brave";
                break;
            case 4:
                lastname1 = "Coward";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname2 = "hammer";
                break;
            case 1:
                lastname2 = "heart";
                break;
            case 2:
                lastname2 = "sword";
                break;
            case 3:
                lastname2 = "shield";
                break;
            case 4:
                lastname2 = "might";
                break;
            }
            lastname = lastname1 + lastname2;
        }else if(race == "elf"){
            switch(r.nextInt(5)){
            case 0:
                surname = "Zevvin";
                break;
            case 1:
                surname = "Lukias";
                break;
            case 2:
                surname = "Biinlas";
                break;
            case 3:
                surname = "Lasthandir";
                break;
            case 4:
                surname = "Iskaal";
                break;
            }
            switch(r.nextInt(5)){
            case 0:
                lastname = "Huulrash";
                break;
            case 1:
                lastname = "Bluurkas";
                break;
            case 2:
                lastname = "Hraataas";
                break;
            case 3:
                lastname = "Lektar";
                break;
            case 4:
                lastname = "Kuulskar";
                break;
            }
        }else{
            race = "dwarf";
            surname = "Ugbor";
            lastname = "Braveshield";
        }
        String[] code;
        code = new String[3];
        code[0] = race;
        code[1] = surname;
        code[2] = lastname;
        return code;
    }

    public static void attack(int[] atkd, int[] atkr){
        Random r = new Random();
        int rand = 2;
        rand = atkr[3] - atkr[2] + 1;
        atkd[0] -= r.nextInt(rand) + 1;
    }

    public static void main(String[] args){
        int option2;
        String option;
        int turn = 0;
        turn = 0;
        int[] atkr;
        int[] atkd;
        String temp;
        int[] nobody;
        nobody = new int[1];
        nobody[0] = 0;
        int[] Char1Sheet = CharacterSheet(8, 8, 1, 4);
        int[] NPC1Sheet = CharacterSheet(15, 15, 1, 4);
        int[] Creature1Sheet = CreatureSheet(40, 40, 1, 4);
        Scanner sc = new Scanner(System.in);
        System.out.println("We kindly ask you to choose only what the program tells you to.");
        System.out.println("Press ENTER to proceed...");
        temp = sc.nextLine();
        System.out.println("Hello, player! Please tell me your character's name:");
        String Char1Name = sc.nextLine();
        System.out.println("Say hello to " + Char1Name + "! What village is it that he lives in?");
        String Village = sc.nextLine();
        String NPC1race = "";
        Random r = new Random();
        switch(r.nextInt(2)){
        case 0:
            NPC1race = "dwarf";
            break;
        case 1:
            NPC1race = "elf";
        }
        String[] NPC1 = NPC(NPC1race);
        System.out.println(Village + ", I see! So, " + Char1Name +" knows a fellow " + NPC1[0] + ", his name is " + NPC1[1] + " " + NPC1[2] + ". They know each other very well.");
        temp = sc.nextLine();
        System.out.println("Today, they both go out into the woods next to " + Village + ", in hopes of getting some wood.");
        temp = sc.nextLine();
        System.out.println("Night falls and " + Char1Name + " and " + NPC1[1] + " are still in the woods. Sadly, they got lost, so they start searching for a shelter.");
        temp = sc.nextLine();
        System.out.println("A grey creature comes out of nowhere and attacks " + Char1Name + "!");
        line();
        attack(Char1Sheet, Creature1Sheet);
        String Creature1Name = "GreyCreature";
        while((NPC1Sheet[0] > 0 && Creature1Sheet[0] > 0) || (Char1Sheet[0] > 0 && Creature1Sheet[0] > 0)){
            System.out.println("Stats:");
            System.out.println(Char1Name + ": " + Char1Sheet[0] + "/" + Char1Sheet[1] + "HP");
            System.out.println(NPC1[1] + ": " + NPC1Sheet[0] + "/" + NPC1Sheet[1] + "HP");
            System.out.println(Creature1Name + ": " + Creature1Sheet[0] + "/" + Creature1Sheet[1] + "HP");
            if(turn % 3 == 0 && Char1Sheet[0] > 0){
                atkr = Char1Sheet;
                atkd = nobody;
                System.out.println("It is " + Char1Name + "'s turn. What does he do?");
                System.out.println("1.(a)ttack");
                option = sc.nextLine();
                if(option.equals("a")){
                    System.out.println(Char1Name + " attacks. Who should he attack?");
                    System.out.println("1.GreyCreature");
                    option2 = sc.nextInt();
                    if(option2 == 1){
                        atkd = Creature1Sheet;
                    }
                    attack(atkd, atkr);
                }
                temp = sc.nextLine();
            }else if(turn % 3 == 1 && NPC1Sheet[0] > 0){
                atkr = NPC1Sheet;
                atkd = nobody;
                System.out.println("It is " + NPC1[1] + "'s turn. What does he do?");
                System.out.println("1.(a)ttack");
                option = sc.nextLine();
                if(option.equals("a")){
                    System.out.println(NPC1[1] + " attacks. Who should he attack?");
                    System.out.println("1.GreyCreature");
                    option2 = sc.nextInt();
                    if(option2 == 1){
                        atkd = Creature1Sheet;
                    }
                    attack(atkd, atkr);
                }
                temp = sc.nextLine();
            }else if(turn % 3 == 2 && Creature1Sheet[0] > 0){
                atkr = Creature1Sheet;
                atkd=nobody;
                switch(r.nextInt(2)){
                case 0:
                    atkd = Char1Sheet;
                    break;
                case 1:
                    atkd = NPC1Sheet;
                }
                if(atkd == Char1Sheet && Char1Sheet[0] > 0){
                    System.out.println(Creature1Name + " attacks " + Char1Name + ".");
                }else{
                    atkd = NPC1Sheet;
                    System.out.println(Creature1Name + " attacks " + NPC1[1] + ".");
                }
                attack(atkd, atkr);
                temp = sc.nextLine();
            }
            turn++;
        }
        if(Char1Sheet[0] <= 0 && NPC1Sheet[0] <= 0){
            System.out.println("Sadly, the two friends did not survive the wildernees... This was the end of our heroes!");
        }else if(Creature1Sheet[0] <= 0){
            System.out.println("The two friends continue their search for shelter.");
        }
        System.out.println("Thank you so much for playing Virtual Sample Session 1 (VSS1) of my game, currently V.Alpha0.0.0!");
        System.out.print(temp);
        temp = sc.nextLine();
        sc.close();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 2015-07-29
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多