当前位置: 首页 > 知识库问答 >
问题:

Java“攻击”行不通

刘瑞
2023-03-14

使用给定的代码:

    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,然后使用它们在值之间给出一个随机数,然后从被攻击者的生命值中减去该数。

共有1个答案

琴光亮
2023-03-14

现在,我;我刚刚意识到我用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();
    }
}
 类似资料:
  • 这样效率高吗?我发现这确实比(int I...代码的标准

  • 开发人员通常直接使用或连接潜在的易受攻击的输入与文件,或假设输入文件是有效的。如果未正确检查数据,则可能导致Web服务器处理或调用易受攻击的内容。 示例 一些经典的例子包括 - 将文件上传到Web目录中。 上传进行调整大小。 上传大文件。 上传包含代码(标签)的文件。 将文件上传到Web目录中。 动手实践 第1步 - 启动WebGoat并导航到恶意文件执行部分。如下所示- 第2步 - 为了测试演示

  • “点击劫持”攻击允许恶意页面 以用户的名义 点击“受害网站”。 许多网站都被黑客以这种方式攻击过,包括 Twitter、Facebook 和 Paypal 等许多网站。当然,它们都已经被修复了。 原理 原理十分简单。 我们以 Facebook 为例,解释点击劫持是如何完成的: 访问者被恶意页面吸引。怎样吸引的不重要。 页面上有一个看起来无害的链接(例如:“变得富有”或者“点我,超好玩!”)。 恶意

  • 在本节中,我们将讨论如何攻击网站。对于攻击网站,我们有两种方法: 我们可以使用攻击迄今为止学到的网站方法的方法。因为我们知道网站安装在计算机上,所以可以像其他任何计算机一样尝试攻击和破解它。但是,我们知道网站安装在计算机上,可以像其他计算机一样尝试攻击和破解它。还可以使用服务器端攻击来查看安装了哪个操作系统,Web服务器或其他应用程序。如果发现了漏洞,我们可以使用它们中的任何一个来访问计算机。 另

  • 一、跨站脚本攻击 二、跨站请求伪造 三、SQL 注入攻击 四、拒绝服务攻击 参考资料 一、跨站脚本攻击 概念 跨站脚本攻击(Cross-Site Scripting, XSS),可以将代码注入到用户浏览的网页上,这种代码包括 HTML 和 JavaScript。 攻击原理 例如有一个论坛网站,攻击者可以在上面发布以下内容: // html location.href="//domain.com/?

  • 在上一节,《Python黑帽编程 3.4 跨越VLAN》中,我们讨论了一般的VLAN中实施攻击的方法,这一节属于扩展内容,简单演示下Cisco特有的DTP协议的攻击方法。 由于条件限制,本节的代码我没有在实际环境下测试,不保证有效,欢迎讨论。 3.5.1 DTP协议 动态中继协议 DTP(Dynamic Trunking Protocol)是一种 Cisco 私有协议。DTP 用于两台交换机的直连