Моя проблема только в этом.Я пытался объявить строку как определенный размер, я сортирую и все такое, но единственное, что сработало, - это поместить строку в метод main внутри используемого мной основного класса.Это расстраивает, потому что я перепробовал все, что мог придумать.Почему элементы становятся недействительными, когда я импортирую их из другого класса?Я добавил основной метод к классу ходов и переместил его туда, чтобы тоже попробовать, но безрезультатно.Вот первая программа:
import java.util.Arrays;
public class movesClass {
public String[] getMoves() {
return moves;
}
String[] moves;
public static String[] useMove(String[] moves) {
moves[0] = "punch";
moves[1] = "kick";
moves[2] = "defend";
moves[3] = "run";
moves[4] = "inventory";
moves[5] = "rickroll";
moves[6] = "heal";
Arrays.sort(moves);
return moves;
}
}
And the body of the main one:
import java.io.*;
import java.util.*;
public class practiceBattle {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
inventoryClass quant = new inventoryClass();
movesClass mov = new movesClass();
String[] moves = mov.getMoves();
int hp = 0;
int playerHp = 200;
String input = "default";
int damage = 0;
int hitDif = 50; //default
int kickChance1 = 0; //default -- goes into hitChance
int kickChance = kickChance1 - hitDif; //chance to hit
int hitChance1 = 0;
int hitChance = hitChance1 - hitDif;
int runDif = 50; //default
int runChance1 = 0; //default -- goes into runChance
int runChance = runChance1 - runDif; //chance to run
int enemyMinAttack = 0;
int enemyAttack = 0;
int index = 0;
int[] levelArray = {1, 2, 3};
String[] monsterArray = {"GNOLL", "TROLL", "DRAGON"};
String monster = monsterArray[rand.nextInt(monsterArray.length)];
int level = rand.nextInt(levelArray.length) + 1;
if(level == 1)
{
System.out.println("YOUR OPPONENT IS A BABY " + monster + "!");
}
else
{
System.out.println("YOUR OPPONENT IS A LEVEL " + level + " " + monster + "!");
}
if(level == 1)
{
hp = rand.nextInt(20) + 41;
enemyAttack = rand.nextInt(5) + 1;
}
else if(level == 2)
{
hp = rand.nextInt(20) + 91;
enemyAttack = rand.nextInt(6) + 5;
}
else if(level == 3)
{
hp = rand.nextInt(20) + 141;
enemyAttack = rand.nextInt(7) + 10;
}
enemyMinAttack = rand.nextInt(enemyAttack) + 1;
System.out.println("YOUR OPPONENT'S HP IS: " + hp + "\n");
int permEnemyAttack = enemyAttack;
int permEnemyMinAttack = enemyMinAttack;
ext:
while(hp > 0)
{
enemyAttack = permEnemyAttack;
enemyMinAttack = permEnemyMinAttack;
do
{
if(playerHp == 0)
{
System.out.println("YOU HAVE DIED. GAME OVER!\n\n\n");
break ext;
}
else
{
System.out.println("Choose an action:\nkick\npunch\nrun\ndefend\n");
input = scan.nextLine();
index = Arrays.binarySearch(moves, input);
System.out.println(index);
if(index < 0)
{
System.out.println("\n\n\nINVALID INPUT -- TRY AGAIN\n\n\n");
}
}
} while(index < 0);
Это то, что у меня есть основной, вплоть до того, где проблема заканчивается и откуда она начинается.Любой inpu будет принята с благодарностью.Спасибо!