내 문제는 바로 그것입니다. 내가 문자열을 특정 크기로 선언하려고했는데, 모든 것이 정렬되었지만 작동하는 유일한 방법은 내가 사용하는 메인 클래스 내부의 main 메소드에 문자열을 넣는 것이다. 내가 생각할 수있는 모든 것을 시도했기 때문에 그것은 실망 스럽다. 다른 클래스에서 가져온 요소가 무효화되는 이유는 무엇입니까? 나는 move 클래스에 main 메소드를 추가하고 그것을 시도해 보려고 움직였다. 그러나 아무 소용이 없었다. 나는 문제가 끝나는 곳까지하고 시작되는 곳에서, 주요 하나가 무엇을 그null 포인터 다른 클래스에서 문자열 [예외] 호출
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는 크게 감사하겠습니다. 고맙습니다!
스택 추적을 제공해 주시겠습니까? – Antonio