[英](Java) How to Overwrite A 2D Array Where I've Already Declared Null Values
[英]How to fix null error when I've already declared the variable
我正在制作 PVP RPG 游戲,顯示框顯示為“null”,而不是我已經聲明的變量。
我已將該變量聲明為用戶的下一個輸入並將該信息存儲在該變量中。 然后當我嘗試顯示變量時,它只顯示“空”,
System.out.println("Welcome, Player One and Player Two!");
delay(1500);
System.out.println("What is your name, Player One?");
playerOne.name = userInput.nextLine();
我已經將 playerOne 聲明為一個新角色(不同的類)
System.out.println("Your turn, " + playerOne.name+".");
if (p1Swordgo == 1) {
這是我想出的問題。 它在同一個主方法中,變量在主方法中聲明,是的,我導入了掃描儀並聲明了變量 userInput
我希望它是用戶輸入的內容,但它提出了 null。 正如我之前所說的,它在同一個 main 方法中,應該不會出錯,但它出現了“null”
import java.util.Random;
import java.util.Scanner;
public class Arena {
Random generator = new Random();
public static void main(String[] args) {
Character playerOne = new Character(10,10,0);
Character playerTwo = new Character(10,10,0);
boolean P1hasClass = false;
boolean P2hasClass = false;
int p1Swordgo = 0;
int p2Alchgo = 0;
int p2Archgo = 0;
Scanner userInput = new Scanner(System.in);
System.out.println("Welcome, Player One and Player Two!");
delay(1500);
System.out.println("What is your name, Player One?");
playerOne.name = userInput.nextLine();
delay(1000);
System.out.println("Hello, " +playerOne.name +".");
delay(1000);
System.out.println("What is your name, Player Two?");
playerTwo.name = userInput.nextLine();
delay(1000);
System.out.println("Hello, " +playerTwo.name +".");
delay(1500);
countdown();
System.out.println("Your turn, " + playerOne.name+".");
if (p1Swordgo == 1) {
if (p2Archgo == 1 || p2Alchgo == 1) {
if (playerOne.move == 1){
System.out.println("What do you want to do?" +'\n' +"1 = Move into range of " +playerTwo.name +'\n' +"2 = Heal" +'\n' +"3 = Forfeit");
int P1Choice = userInput.nextInt();
if (P1Choice == 1) {
playerOne.move --;
System.out.println(playerOne.move);
}
}
}
}
}
public static void delay ( int time){
try {
Thread.sleep(time);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
public static void countdown() {
delay(500);
System.out.println("Get ready to fight in 5,");
delay(1000);
System.out.println("4");
delay(1000);
System.out.println("3");
delay(1000);
System.out.println("2");
delay(1000);
System.out.println("1");
delay(1000);
System.out.println("Fight!");
delay(750);
}
}
然后在一個叫做 Character 的類中
public class Character {
public int strength;
public double health;
public int move;
public String name;
public Character(double health, int strength, int move) {
this.health = health;
this.strength = strength;
this.name = name;
this.move = move;
}
}
在一個叫做SwordFighter的課程中
public class SwordFighter extends Character {
public SwordFighter() {
super(60,15, 1);
}
}
在一個叫做 Archer 的班級里
public class Archer extends Character{
public Archer() {
super(45,20, 0);
}
}
最后,在一個名為 Alchemist public class Alchemist extends Character {
public Alchemist() {
super(50,15, 0);
}
}
順便說一下,感謝您的耐心等待
一旦兩個玩家選擇了他們的名字並且你已經使用playerOne.name = userInput.nextLine();
設置了它playerOne.name = userInput.nextLine();
,您分配一個不同的對象,名稱為空,給playerOne
:
playerOne = new SwordFighter();
因此,在執行此行后, playerOne.name
為空。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.