gpt4 book ai didi

java - 名称输出显示 "null"Java

转载 作者:行者123 更新时间:2023-11-30 03:11:29 24 4
gpt4 key购买 nike

我正在制作一个游戏类型的东西,结合了三个类。 不是家庭作业;爱好。

三个类的代码:

运行者:

  public class CounterGameRunner
{
// instance variables - replace the example below with your own
public static void main(String [] args){
Scanner input = new Scanner(System.in);

CounterGameCounter game = new CounterGameCounter();

System.out.println("You want to play a game I see. What is your name?");

String name = input.next();

game.NameIn(name);


CounterGAME game1 = new CounterGAME();

game1.actual();

}
}

实际游戏:

public class CounterGAME
{
// instance variables - replace the example below with your own

Scanner input = new Scanner(System.in);

int number;
int count=1;
boolean loop = true;
public CounterGAME(){


}

public void actual(){

CounterGameCounter game2 = new CounterGameCounter();


System.out.println("Guess a number between 1 and 101, see how many times you get it!");

number=input.nextInt();
int r = (int)(Math.random() * (100) + 1);
while(loop==true){


if(number < r){
System.out.println("Too small, try again");
number = input.nextInt();
count++;
game2.Counter(count);

} else if(number == r){
System.out.println("Wow, you won! Who'd have thought?");
count++;
game2.Counter(count);
break;

System.out.println(game2.done());

} else if(number > r){
System.out.println("Too large, try again");
number = input.nextInt();

count++;

game2.Counter(count);
}


}





}



}

计数器类别:

 public class CounterGameCounter
{
// instance variables - replace the example below with your own



private String Name;

String done1;
int correct;

public CounterGameCounter(){



}



public String NameIn (String nm){
Name = nm;
return Name;
}

public String NameOut(){

return Name;

}


public void Counter(int count){

correct = count;

}

public int getCount(){



return correct;

}


public String done(){
done1 = "Name: " + NameOut() + "\n" +
"Times Answered: " + getCount();




return done1;
}



}

问题:

计数器工作正常,其他所有内容最终都正常显示和运行。但是,我在开始时输入的任何名称在运行程序时始终显示“null”。为什么?

最佳答案

你的变量名确实很困惑,而且你的代码中有很多不好的做法,但是名称中的null是因为你在中创建了一个新的Counter >CounterGAME:

public void actual(){    
// here
CounterGameCounter game2 = new CounterGameCounter();
// more code
}

更改实际值以接收 CounterGameCounter:

public void actual(CounterGameCounter game2){
// more code
}

并称其为:

public static void main(String [] args){
Scanner input = new Scanner(System.in);
CounterGameCounter game = new CounterGameCounter();
System.out.println("You want to play a game I see. What is your name?");
String name = input.next();
game.NameIn(name);
CounterGAME game1 = new CounterGAME();
game1.actual(game);
// more stuff
}
<小时/>

免费提示:

  • 使用String getName()void setName(String)
  • 变量、对象和属性名称以小写开头

    String name;
    Object object;
  • 变量名称必须具有代表性和描述性

    CounterGameCounter counterGameCounter = new CounterGameCounter();

    这也适用于对象名称:

    GameCounter gameCounter = new CounterGameCounter();

关于java - 名称输出显示 "null"Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549458/

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