所以我正在製作一個基於文本的遊戲,並且我想使用一個隨機數生成器來說明它生成的隨機數是3,它會說某個確定的怪物出現了。由於某些原因,它無法讓我爲隨機數做if語句。以下是代碼:如何使用隨機數發生器與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!!");
}**
}
}
}
你正在擲三骰,機率很高,其中一個是三。大約75%的時間你會得到至少一個怪物... – Kostronor 2014-09-11 04:44:42
你看過幫助中心嗎? – 2014-09-11 04:45:19
我知道,但我只是使用這些數字作爲測試。我想要一個怪物很多時間 – 2014-09-11 04:45:25