gpt4 book ai didi

java - 为什么这个打印是NULL?

转载 作者:行者123 更新时间:2023-12-01 07:51:04 27 4
gpt4 key购买 nike

我想知道是否有人可以告诉我为什么在运行它并设置名称时得到输出“NULL”。我已经完成了一个返回方法来从 setName 返回名称,但它仍然显示为 null,就好像它们尚未设置一样。

应该发生的是 - 运行主菜单 - 设置名称 - 然后运行 ​​gameBoard.java 中的 Play 方法并查看其中的名称 - 但我只能看到 null。

主类:

import java.lang.reflect.Array;
import java.util.Random;
import java.util.Scanner;

public class main {


public static void main(String[] args) {

setName SN = new setName();
gameBoard GB = new gameBoard();
Scanner user_input = new Scanner(System.in);
int mainMenuChoice = 0;
String p1Name = null;
String p2Name = null;

while(mainMenuChoice >= 0 && mainMenuChoice < 3){

if(mainMenuChoice == 0){
if(p1Name != null){
System.out.println("Welcome " + p1Name + " and " + p2Name + " to AQADo! ");
}
System.out.println("MAIN MENU");
System.out.println("1. Enter Player Names");
System.out.println("2. Play Game");
System.out.println("3. Quit");
System.out.println("Select your number");
mainMenuChoice = Integer.parseInt(user_input.next());
}
if(mainMenuChoice == 1){
p1Name = SN.setName1();
p2Name = SN.setName2();
mainMenuChoice = 0;
}
if(mainMenuChoice == 2){
int menu = 1;
int[] p1score = {};
Random rn = new Random();
GB.board();
GB.play();
String test = user_input.next();
for(int i = 0; i < 50; i++){
p1score[0] = rn.nextInt(6);


mainMenuChoice = 0;

}

}


}



}
}

setName 类:

    import java.util.Scanner;

public class setName {
public String GlobalP1;
public String GlobalP2;


public String setName1(){
Scanner user_input = new Scanner(System.in);
String p11Name;
System.out.println("Enter the first players name:");
p11Name= user_input.next();
GlobalP1 = p11Name;

return p11Name;
}
public String setName2(){
Scanner user_input = new Scanner(System.in);
String p22Name;
System.out.println("Enter the second players name:");
p22Name= user_input.next();
GlobalP1 = p22Name;


return p22Name;
}
public String GBName(){
return GlobalP1;
}
public String GBName2(){
return GlobalP2;
}
}

游戏板类:

public class gameBoard {

setName SN = new setName();

char[] array1 = new char[11];{
array1[0] = 'x';
array1[1] = 'x';
array1[2] = 'x';
array1[3] = 'x';
array1[4] = 'x';
array1[5] = 'x';
array1[6] = 'x';
array1[7] = 'x';
array1[8] = 'x';
array1[9] = 'x';
array1[10] = 'x';
}
char[] array2 = new char[11];{
array2[0] = 'x';
array2[1] = 'x';
array2[2] = 'x';
array2[3] = 'x';
array2[4] = 'x';
array2[5] = 'x';
array2[6] = 'x';
array2[7] = 'x';
array2[8] = 'x';
array2[9] = 'x';
array2[10] = 'x';
}

public void board(){


System.out.println(" 1 2 3 4 (5) 6 7 8 9 10 11");
System.out.println("Player 1: " + array1[0]+ " " + array1[1] + " " + array1[2]+ " " + array1[3] + " " + array1[4]+ " " + array1[5] + " " + array1[6]+ " " + array1[7] + " " + array1[8]+ " " + array1[9] + " " + array1[10]);
System.out.println("Player 2: " + array2[0]+ " " + array2[1] + " " + array2[2]+ " " + array2[3] + " " + array2[4]+ " " + array2[5] + " " + array2[6]+ " " + array2[7] + " " + array2[8]+ " " + array2[9] + " " + array2[10]);


}
public void play(){
String p1Name = SN.GBName();
String p2Name = SN.GBName2();
String pArray[] = {p1Name,p2Name};


System.out.println(p1Name + " test");

}
}

控制台输出:

    MAIN MENU
1. Enter Player Names
2. Play Game
3. Quit
Select your number
1
Enter the first players name:
Jordan
Enter the second players name:
David
Welcome Jordan and David to AQADo!
MAIN MENU
1. Enter Player Names
2. Play Game
3. Quit
Select your number
2
1 2 3 4 (5) 6 7 8 9 10 11
Player 1: x x x x x x x x x x x
Player 2: x x x x x x x x x x x
null test

最佳答案

在您的 main 类中,您创建一个 setName 实例,并在该实例上初始化名称。然而,在您的类 gameBoard 中,您还创建了一个 setName 的新实例,该实例没有初始化名称,因此您会打印出 null

一个简单的解决方法是将 main 中创建的 SN 对象作为构造函数参数传递给 gameBoard:

gameBoard GB = new gameBoard(SN);

这意味着 gameBoard 应该定义类似这样的构造函数:

class gameBoard {
setName SN;
public gameBoard(setName sn) {
this.SN = sn;
}
...

关于java - 为什么这个打印是NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com