Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 陷入僵局_Java_Random - Fatal编程技术网

Java 陷入僵局

Java 陷入僵局,java,random,Java,Random,你好,我现在正在做一个奇怪的游戏,其中一部分你会遇到一个叫做老鼠的怪物,我用随机数给出一个介于0-7之间的数字来造成伤害,每次你点击它应该攻击(造成伤害)并用上面的随机数来决定它造成了多少伤害。每次它造成伤害时,我希望它从老鼠的整体健康中减去 int rabbithealth = 23; Random attack = new Random(); rabbithealth = rabbithealth - attack.nextInt(7); 现在,我知道我为什么有麻烦了。rabbith

你好,我现在正在做一个奇怪的游戏,其中一部分你会遇到一个叫做老鼠的怪物,我用随机数给出一个介于0-7之间的数字来造成伤害,每次你点击它应该攻击(造成伤害)并用上面的随机数来决定它造成了多少伤害。每次它造成伤害时,我希望它从老鼠的整体健康中减去

int rabbithealth = 23;
Random attack = new Random();   
rabbithealth = rabbithealth - attack.nextInt(7);
现在,我知道我为什么有麻烦了。rabbithealth是23,所以它每次都会减去随机麻木,所以它只会打印rabbithealth-attack,也就是说,但是我希望它每次都从这个数字中减去,而不是23。举个例子 而不是它是23-4,或23-6,每次我想它是23-3,然后再次点击20-(随机数) 这是我的密码 包com.Game.user

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import java.awt.Font;
import java.awt.Canvas;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;

public class Swendiver {

    private JFrame frmSwendiver;
    private JTextField console;
    private JTextField Log;
    private JTextField Weapon;
    private JTextField Arrows;
    private JTextField next;
    private JTextField Class;
    private JTextField Coin;
    private JTextField enemyhealth;
    private JTextField myhealth;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Swendiver window = new Swendiver();
                    window.frmSwendiver.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the application.
     */
    public Swendiver() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmSwendiver = new JFrame();
        frmSwendiver.setResizable(false);
        frmSwendiver.setTitle("Swendiver 1.0");
        frmSwendiver.setAutoRequestFocus(false);
        frmSwendiver.setAlwaysOnTop(true);
        frmSwendiver.setBounds(100, 100, 623, 406);
        frmSwendiver.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmSwendiver.getContentPane().setLayout(null);

        console = new JTextField();
        console.setBounds(0, 0, 606, 44);
        console.setEditable(false);
        frmSwendiver.getContentPane().add(console);
        console.setColumns(10);

        Log = new JTextField();
        Log.setBounds(0, 311, 607, 56);
        Log.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                // KeyEvent.getKeyCode(VK_ENTE-R);
            }
        });
        frmSwendiver.getContentPane().add(Log);
        Log.setColumns(10);
        JButton button4 = new JButton("Enter");
        button4.setBounds(517, 43, 89, 270);
        JLabel lblHealth = new JLabel("Your Health:");
        lblHealth.setBackground(new Color(50, 205, 50));
        lblHealth.setBounds(275, 93, 100, 27);
        frmSwendiver.getContentPane().add(lblHealth);
        console.setText("Press The enter button to start Swendiver");
        JButton Send = new JButton("Enter");
        Send.setBounds(517, 43, 89, 270);
        Send.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        Send.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                boolean isClicked = true;
                if (isClicked == true) {
                    isClicked = false;

                    lblHealth.setName("Health: 75%");
                    console.setText("[Hermin] Welcome. Ready for your adventure? (Click Enter)");
                    Send.setVisible(false);
                }
            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                Log.setText("");
                ArrayList<String> weapon = new ArrayList<String>();
                console.setText("Ok, choose your weapon. <SWORD>||<BOW>");
                // Log.getText().equalsIgnoreCase("SWORD")
                // console.setText("Good choice.");
                Log.setText("");
                weapon.add("Sword");
                if (weapon.get(0).equalsIgnoreCase("Sword")) {
                    console.setText("[Hermin] A sword? I see. Good choice...");
                    next.setText("This box is used for Instructions/Actions");
                }
            }
        });

        frmSwendiver.getContentPane().add(Send);

        JLabel lblWeapon = new JLabel("Weapon:");
        lblWeapon.setBounds(441, 145, 56, 27);
        frmSwendiver.getContentPane().add(lblWeapon);
        JButton button3 = new JButton("Enter");
        button3.setBounds(517, 43, 89, 270);
        Weapon = new JTextField();
        Weapon.setBounds(421, 166, 86, 20);
        Weapon.setEditable(false);
        frmSwendiver.getContentPane().add(Weapon);
        Weapon.setColumns(10);

        JButton Enter = new JButton("Enter");
        Enter.setBounds(517, 41, 89, 272);
        frmSwendiver.getContentPane().add(Enter);

        JLabel lblArrows = new JLabel("Arrows:");
        lblArrows.setEnabled(false);
        lblArrows.setBounds(275, 151, 46, 14);
        frmSwendiver.getContentPane().add(lblArrows);

        Arrows = new JTextField();
        Arrows.setEnabled(false);
        Arrows.setBounds(260, 168, 68, 20);
        Arrows.setEditable(false);
        frmSwendiver.getContentPane().add(Arrows);
        Arrows.setColumns(10);
        Random attack = new Random();
        next = new JTextField();
        next.setBounds(0, 43, 346, 25);
        next.setEditable(false);
        frmSwendiver.getContentPane().add(next);
        next.setColumns(10);

        JButton Attack = new JButton("Attack");

        Attack.setBounds(0, 68, 89, 245);
        frmSwendiver.getContentPane().add(Attack);

        JLabel lblClass = new JLabel("Class:");
        lblClass.setBounds(451, 93, 46, 27);
        frmSwendiver.getContentPane().add(lblClass);

        Class = new JTextField();
        Class.setEditable(false);
        Class.setBounds(421, 114, 86, 20);
        frmSwendiver.getContentPane().add(Class);
        Class.setColumns(10);
        JLabel swordpic = new JLabel("");
        swordpic.setEnabled(false);
        swordpic.setIcon(new ImageIcon("C:\\Users\\Owner\\Desktop\\IDE\\Swendiver\\bin\\sword-icon.png"));
        swordpic.setBounds(391, 154, 40, 34);

        JLabel bowpic = new JLabel("");
        bowpic.setEnabled(false);
        bowpic.setIcon(new ImageIcon("C:\\Users\\Owner\\Desktop\\Crossbow-icon.png"));
        bowpic.setBounds(352, 152, 40, 34);
        frmSwendiver.getContentPane().add(bowpic);
        frmSwendiver.getContentPane().add(swordpic);
        Coin = new JTextField();
        Coin.setText("78");
        Coin.setEditable(false);
        Coin.setBounds(421, 211, 86, 20);
        frmSwendiver.getContentPane().add(Coin);
        Coin.setColumns(10);
        next.setText("");
        JLabel lblCoin = new JLabel("Coin:");
        lblCoin.setBounds(451, 198, 46, 14);
        frmSwendiver.getContentPane().add(lblCoin);
        Enter.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

                console.setText("[Hermin]Choose your class <Warrior> or <Archer>");
                if (Log.getText().equalsIgnoreCase("Warrior")) {
                    console.setText("[Hermin]A Swordsman.. I see. Good choice.");
                    Weapon.setText("Sword");
                    next.setEnabled(true);
                    next.setText("Click Enter to continue");
                    Log.setText("");
                    Class.setText("Warrior");
                    Enter.setVisible(false);
                    Enter.setEnabled(false);
                    swordpic.setEnabled(true);
                }
                if (Log.getText().equalsIgnoreCase("Archer")) {
                    console.setText("[Hermin]An Archer.. Interesting. Here are your Items.");
                    Weapon.setText("Long-Bow");
                    Arrows.setText("19");
                    next.setEnabled(true);
                    next.setText("Click Enter to continue");
                    Log.setText("");
                    Class.setText("Archer");
                    lblArrows.setEnabled(true);
                    Arrows.setEnabled(true);
                    Enter.setVisible(false);
                    Enter.setEnabled(false);
                    button3.setEnabled(true);
                    button3.setVisible(true);
                    bowpic.setEnabled(true);
                }
                if (Log.getText().equalsIgnoreCase("/secretcommand")) {
                    Coin.setText(Integer.toString(300));
                    int Coin = 300;
                    next.setText("222 Coin Aquired");
                    next.setEnabled(true);
                    Log.setText("");
                    next.setText("Click Enter to continue!");
                    Enter.setEnabled(false);
                    Enter.setVisible(false);
                    button3.setVisible(true);
                    button3.setEnabled(true);
                }

            }

        });

        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                console.setText("[Hermin] Great. Welcome then. Head into SheildsTown to collect some items.");
                next.setText("/EquipArmor to get geared and go.");
                if (Log.getText().equalsIgnoreCase("/EquipArmor")) {
                    next.setText("You leave the armory.");
                    console.setText("");
                    Log.setText("");
                    button3.setEnabled(false);
                    button3.setVisible(false);
                    button4.setVisible(true);
                    button4.setEnabled(true);
                }
            }
        });
        frmSwendiver.getContentPane().add(button3);

        int rathealth = 23;
        button4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                next.setText("You encounter a rat! Attack it! [Rat Health]: 23");
                console.setText("[You]: Ahh! I shall kill you wretched beast!");
                Attack.setEnabled(true);
                boolean mob = true;
                if (rathealth == 0) {

                    next.setText("You Killed the Rat!");
                    console.setText("I need food for later. </Loot to scavenge monsters or people you kill for items>");
                }

                    }
                });

        frmSwendiver.getContentPane().add(button4);

        JLabel lblNewLabel = new JLabel("Enemies health:");
        lblNewLabel.setBounds(125, 119, 100, 27);
        frmSwendiver.getContentPane().add(lblNewLabel);

        enemyhealth = new JTextField();
        enemyhealth.setEditable(false);
        enemyhealth.setBounds(113, 139, 96, 20);
        frmSwendiver.getContentPane().add(enemyhealth);
        enemyhealth.setColumns(10);

        JLabel lblEnemyDetails = new JLabel("Enemy details");
        lblEnemyDetails.setFont(new Font("Tahoma", Font.PLAIN, 18));
        lblEnemyDetails.setBounds(115, 79, 128, 25);
        frmSwendiver.getContentPane().add(lblEnemyDetails);

        JLabel lblYourDetails = new JLabel("Your details");
        lblYourDetails.setFont(new Font("Tahoma", Font.PLAIN, 18));
        lblYourDetails.setBounds(389, 55, 129, 41);
        frmSwendiver.getContentPane().add(lblYourDetails);

        myhealth = new JTextField();
        myhealth.setText("           100%");
        myhealth.setEditable(false);
        myhealth.setBounds(257, 114, 100, 20);
        frmSwendiver.getContentPane().add(myhealth);
        myhealth.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("Commands/input below");
        lblNewLabel_1.setBounds(241, 286, 161, 27);
        frmSwendiver.getContentPane().add(lblNewLabel_1);

        boolean enemydoesdamage = true;
        String middlespacingforhealth = "           ";
        Attack.addMouseListener(new MouseAdapter() {
            boolean attacksituation = true;
            int noobiesword = attack.nextInt(7);
            @Override
            public void mouseClicked(MouseEvent e) {
                if (Weapon.getText().equalsIgnoreCase("Sword")) {
                //  next.setText("You swing your sword and do: "+ Integer.toString(attack.nextInt()) + " damage");
                    Attack.setEnabled(true);
                    enemyhealth.setText(Integer.toString(rathealth - attack.nextInt(7)));
                //  next.setText("Rat bites, and does: " + Integer.toString(attack.nextInt(9))+" damage");
                    console.setText("[Thought](My arm is dripping blood)");

                    if(enemydoesdamage){
                        int Health = 100;
                        myhealth.setText("           "+ Integer.toString(Health-attack.nextInt(9))+"%");
                    }
                }
                if(Weapon.getText().equalsIgnoreCase("Long-Bow")){
                    next.setText("You shoot your bow and do: " + noobiesword);
                    enemyhealth.setText("           " + Integer.toString( rathealth - noobiesword));
                }
                }


        });

}}
导入java.awt.EventQueue;
导入javax.swing.JFrame;
导入javax.swing.JTextField;
导入java.awt.BorderLayout;
导入javax.swing.JButton;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.util.ArrayList;
导入javax.swing.JLabel;
导入java.awt.event.KeyAdapter;
导入java.awt.event.KeyEvent;
导入javax.swing.JTextArea;
导入java.awt.Color;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入java.util.Random;
导入java.awt.Font;
导入java.awt.Canvas;
导入javax.swing.ImageIcon;
导入javax.swing.JToggleButton;
公开级宣誓人{
私人JFrame frmSwendiver;
专用JTextField控制台;
私有JTextField日志;
私人野战武器;
私有JTextField箭头;
私有JTextField其次;
私有JTextField类;
私人杰特斯菲尔德硬币;
私营医疗卫生机构;
私营医疗机构;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
Swendiver窗口=新Swendiver();
window.frmSwendiver.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公开宣誓{
初始化();
}
/**
*初始化框架的内容。
*/
私有void初始化(){
frmSwendiver=新JFrame();
frmSwendiver.setresizeable(false);
frmSwendiver.setTitle(“Swendiver 1.0”);
frmSwendiver.setAutoRequestFocus(假);
frmSwendiver.setAlwaysOnTop(真);
frmSwendiver.setBounds(100100623406);
frmSwendiver.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmSwendiver.getContentPane().setLayout(null);
控制台=新的JTextField();
控制台.立根(0,0,606,44);
console.setEditable(false);
frmSwendiver.getContentPane().add(控制台);
控制台。设置列(10);
Log=新的JTextField();
对数立根(0,311,607,56);
Log.addKeyListener(新的KeyAdapter(){
@凌驾
按下公共无效键(按键事件e){
//KeyEvent.getKeyCode(VK_ENTE-R);
}
});
frmSwendiver.getContentPane().add(日志);
Log.setColumns(10);
JButton button4=新JButton(“输入”);
按钮4.立根(517、43、89、270);
JLabel lblHealth=新JLabel(“您的健康:”);
LBL健康挫折背景(新颜色(50205,50));
LBL健康挫折(275,93100,27);
frmSwendiver.getContentPane().add(lblHealth);
console.setText(“按enter按钮启动Swendiver”);
JButton Send=新JButton(“输入”);
发送.挫折(517,43,89270);
Send.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
}
});
Send.addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseClicked(MouseEvent e){
布尔值isClicked=true;
如果(isClicked==真){
isClicked=false;
lblHealth.setName(“健康:75%”);
console.setText(“[Hermin]欢迎。准备好冒险了吗?(单击Enter)”;
Send.setVisible(false);
}
}
@凌驾
public void mousePressed(MouseEvent arg0){
Log.setText(“”);
ArrayList武器=新的ArrayList();
console.setText(“好的,选择你的武器.| |”);
//Log.getText().equalsIgnoreCase(“剑”)
//console.setText(“好选择”);
Log.setText(“”);
武器。添加(“剑”);
if(武器获取(0.equalsIgnoreCase(“剑”)){
console.setText(“[赫敏]一把剑?我明白了。不错的选择……”);
next.setText(“此框用于说明/操作”);
}
}
});
frmSwendiver.getContentPane().add(发送);
JLabel lblWeapon=新的JLabel(“武器”);
lblWeapon.立根(441,145,56,27);
frmSwendiver.getContentPane().add(lblWeapon);
JButton button3=新JButton(“输入”);
按钮3.立根(517,43,89,270);
武器=新的JTextField();
武器。挫折(4211668620);
武器。设置可编辑(假);
frmSwendiver.getContentPane().add(武器);
武器装备(10);
JButton Enter=新JButton(“Enter”);
输入.setBounds(517,41,89272);
frmSwendiver.getContentPane().add(输入);
JLabel lblArrows=新的JLabel(“箭头:”);
lblArrows.setEnabled(false);
lblArrows.挫折(275151 46 14);
frmSwendiver.getContentPane().add(lblArrows);
箭头=新的JTextField();
箭头。设置启用(错误);
Arrows.se
public interface Monster {
    public int getHitPoints();
    public boolean isDead();
    public int takeDamage(int damage);
    public int performAttack();
}
public abstract class AbstractMonster implements Monster {

    private int hitPoints;

    public AbstractMonster(int hitPoints) {
        setHitPoints(hitPoints);
    }

    @Override
    public int getHitPoints() {
        return hitPoints;
    }

    @Override
    public boolean isDead() {
        return getHitPoints() <= 0;
    }

    @Override
    public int takeDamage(int damage) {
        setHitPoints(getHitPoints() - damage);
        return getHitPoints();
    }

    protected void setHitPoints(int hp) {
        hitPoints = hp;
    }

}
public class Rat extends AbstractMonster {

    public Rat() {
        super(5 + (int) (Math.random() * 15));
    }

    @Override
    public int performAttack() {
        return (int) (Math.random() * 3);
    }

}
System.out.println("You've been attacked by vicious rat!");
Rat rat = new Rat();
Random rnd = new Random();
do {
    System.out.println("! Whack");
    int damage = rnd.nextInt(4);
    System.out.println("You inflict " + damage + " points of damage on the foul creature");
    if (rat.takeDamage(damage) > 0) {
        System.out.println("The rat attacks and inflicts a merger " + rat.performAttack() + " points of damage on you");
    }
} while (!rat.isDead());
System.out.println("");
System.out.println("The vial vermon has been slain!");
System.out.println("You will forever been known as the 'Slayer of Rats'!");
You've been attacked by vicious rat!
! Whack
You inflict 2 points of damage on the foul creature
The rat attacks and inflicts a merger 0 points of damage on you
! Whack
You inflict 2 points of damage on the foul creature
The rat attacks and inflicts a merger 1 points of damage on you
! Whack
You inflict 3 points of damage on the foul creature
The rat attacks and inflicts a merger 1 points of damage on you
! Whack
You inflict 0 points of damage on the foul creature
The rat attacks and inflicts a merger 1 points of damage on you
! Whack
You inflict 1 points of damage on the foul creature
The rat attacks and inflicts a merger 1 points of damage on you
! Whack
You inflict 2 points of damage on the foul creature
The rat attacks and inflicts a merger 0 points of damage on you
! Whack
You inflict 3 points of damage on the foul creature
The rat attacks and inflicts a merger 0 points of damage on you
! Whack
You inflict 2 points of damage on the foul creature

The vial vermon has been slain!
You will forever been known as the 'Slayer of Rats'!