如何使用if语句的随机数生成器

时间:2014-09-11 04:40:07

标签: java random numbers

所以我正在制作一个基于文本的游戏,我想用一个随机数生成器来说明它生成的随机数是3,它会说某个怪物出现了。由于某种原因,它无法允许我为随机数做一个if语句。这是代码:

import java.util.Random;
import java.util.Scanner;
public class mainClass 
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        String username, character_type, testing_enemy;

        //Variables for spells
        double mage_fire, mage_iceblast, mage_voidray;
        double warrior_uppercut, warrior_kick, warrior_hook;
        double archer_diamond, archer_power, archer_precision;

        //variables for base stats
        int base_health, base_spell_dmg, base_attack_dmg, base_mana;

        //variable for random number generator
        int number = Integer.parseInt(testing_enemy);

        System.out.println("Welcome to Realm Of Worlds!");
        System.out.println("Enter username: ");
        username = input.nextLine();
        System.out.println();
        System.out.println("Welcome to Realm Of Worlds " + username);
        System.out.println("Realm Of Worlds is an interactive text based game "
                + "that allows you to fight monsters and buy items and spells to get even stronger.");
        System.out.println("Would you like to be a Mage, a Warrior, or an Archer?");
        character_type = input.nextLine();


        if(character_type.equals("mage") || character_type.equals("Mage"))
        {
            System.out.println("Ok, " + username + " you are now a " + character_type + "!");

            base_health = 850;
            base_spell_dmg = 60;
            base_attack_dmg = 50;
            base_mana = 500;

            System.out.println("As a Mage, you have " + base_health + " base health, " + base_spell_dmg + " base spell damage, " + base_attack_dmg + " base attack damage, and " + base_mana + " base mana.");
            System.out.println("Your spells are Fire Blast, Ice Blast, and Void Ray.");
            System.out.println("All of your spells do Spell Damage, so focus on building Spell Damage Items");

            mage_fire = (30 + (.60*base_spell_dmg));
            mage_iceblast = (50 + (.45*base_spell_dmg));
            mage_voidray = (45 + (.55*base_spell_dmg));

            System.out.println("Would you like to use 1. Fire Blast, 2. Ice Blast, 3. Void Ray? (Just type 1, 2, or 3)");

        }


        if(character_type.equals("warrior") || character_type.equals("Warrior"))
        {
            System.out.println("Ok, " + username + " you are now a " + character_type + "!");

            base_health = 1000;
            base_spell_dmg = 20;
            base_attack_dmg = 80;
            base_mana = 300;

            System.out.println("As a Warrior, you have " + base_health + " base health, " + base_spell_dmg + " base spell damage, " + base_attack_dmg + " base attack damage, and " + base_mana + " base mana.");
            System.out.println("Your spells are Uppercut, Kick, and Hook Punch");
            System.out.println("All of your spells do Attack Damage, so focus on building Attack Damage Items");

            warrior_uppercut = (30 + (.55*base_attack_dmg));
            warrior_kick = (50 + (.45*base_attack_dmg));
            warrior_hook = (45 + (.50*base_attack_dmg));
        }

        if(character_type.equals("archer") || character_type.equals("Archer"))
        {
            System.out.println("Ok, " + username + " you are now a " + character_type + "!");

            base_health = 700;
            base_spell_dmg = 40;
            base_attack_dmg = 70;
            base_mana = 400;

            System.out.println("As an Archer, you have " + base_health + " base health, " + base_spell_dmg + " base spell damage, " + base_attack_dmg + " base attack damage, and " + base_mana + " base mana.");
            System.out.println("Your spells are Diamond Arrow, Power Shot, and Precision Eye");
            System.out.println("Also, as archer, you have a passive ability of dodging 1 in every 6 attacks from an opponent");
            System.out.println("Your spells do a combination of Spell and Attack Damaeg, so it is worth building both types of items, but more Attack Damage is done than Spell Damage");

            archer_diamond = (30 + ((.35*base_attack_dmg)+(.20*base_spell_dmg)));
            archer_power = (50 + ((.30*base_attack_dmg)+(.15*base_spell_dmg)));
            archer_precision = (45 + ((.35*base_attack_dmg)+(.15*base_spell_dmg)));
        }

        //Setting up enemy randomization
        **Random dice = new Random();

        for(int counter=1; counter<=3; counter++)
        {
            number = 1+dice.nextInt(3);

            if(number.equals(3))
            {
                System.out.println("A monster appeared!!");
            }**
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您正在使用接受对象的equals。但是int不是对象。尝试==代替。

if (number == 3)

答案 1 :(得分:0)

尝试使用if (number == 3)代替if (number.equals(3))。通常,var.equals()用于对象和字符串。比较数值(整数和浮点)可以使用==

答案 2 :(得分:0)

让我们举个例子。

假设我希望生成5-10之间的数字。

  int max=10;
    int min=5;
    int diff=max-min;
    Random rn = new Random();
    int i = rn.nextInt(diff+1);
    i+=min;
    System.out.print("The Random Number is " + i);

让我们理解这个

使用最高值消除最大值,使用最小值消除最小值。

现在,我们需要确定可以获得多少个可能的值。对于这个例子,它将是

5, 6, 7, 8, 9, 10

所以,这将是max-min + 1的计数。

i.e. 10-5+1=6

随机数将生成0-5之间的数字。

i.e. 0, 1, 2, 3, 4, 5

将最小值添加到随机数将产生

5, 6, 7, 8, 9, 10

因此我们获得了所需的范围。