在JAVA中将非静态变量转换为静态变量?
只上了第三周的课(编程新手)。 我正在用Java编写一个基于文本的故事,但在这个过程中我遇到了困难。我有一个名为“static String dogName;”的静态变量,我试图更改它的值(只更改一次)。在游戏开始时,用户可以选择给他们的狗命名。当我尝试给狗命名时,由于静态字符串dogName,代码跳过命名提示在JAVA中将非静态变量转换为静态变量?,java,Java,只上了第三周的课(编程新手)。 我正在用Java编写一个基于文本的故事,但在这个过程中我遇到了困难。我有一个名为“static String dogName;”的静态变量,我试图更改它的值(只更改一次)。在游戏开始时,用户可以选择给他们的狗命名。当我尝试给狗命名时,由于静态字符串dogName,代码跳过命名提示 我想让用户选择命名他们的狗 如果在我的代码中有更好的方法,请让我知道 部分代码可能不像决策那样完整 public static Scanner keyboard = new Scanne
public static Scanner keyboard = new Scanner(System.in);
public static int choice;
// dogName is Dogs name forever a hundred times rick & morty
static String dogName;
public static void main(String[] args) {
int karma = 0;
// Dog stuff...
Dog Dandy;
Dandy = new Dog();
// Prologue
System.out.println("You're walking through an alley late at night "
+ ", you see a stray dog. What do you do? ");
System.out.println("[1] Approach");
System.out.println("[2] Attempt to touch");
System.out.println("[3] Give treat");
boolean running = true;
GAME:
while (running) {
choice = keyboard.nextInt();
switch (choice) {
case 1:
System.out.println("The dog became alarmed!");
Dandy.bark();
break;
case 2:
System.out.println("The dog becomes aggressive!");
Dandy.bite();
break;
case 3:
System.out.println("The dog comes in peace");
Dandy.sit();
break;
}
if (choice == 1) {
System.out.println("You stand back in caution. You cannot risk being bitten.");
}
if (choice == 2) {
System.out.print("");
karma--;
}
if (choice == 3) {
System.out.println("You give the dog a treat. It wags its tail in excitement");
karma++;
}
// Chapter 1.1 - Man's best friend
System.out.println("\nThe dog will live a harsh life in the outside world. What would you like to do? "
+ "\n[1] Adopt dog\n[2] Leave dog\n[3] Quit game! You're bored...");
choice = keyboard.nextInt();
switch (choice) {
case 1:
System.out.println("\nYou welcome your new companion");
System.out.println("\nWould you like to give him a name?\n[1] No\n[2] Yes");
choice = keyboard.nextInt();
switch (choice){
case 1:
System.out.println("You see a shiny object beneath his foot, it's a dog collar."
+ "\nYou pick up the dog collar and see the name Todd on it."
+ "\nYes, because you did not choose a name for your dog, we gave him the most basic name ever. "
+ "You're welcome.");
dogName = "Todd"; //RIP doge
karma--;
break;
case 2:
dogName = keyboard.nextLine();
karma++;
}
}
// Good guy player gives his dog a name
// Chapter 1.2 - Home sweet home
System.out.println("\n" + dogName + " crawls up to your leg and lets out a whimper.\n"
+ "Is " + dogName + " just afraid of the dark, or is he hungry?"
+ "\nYou don't know the last time he ate. What will you do?");
System.out.println("\n[1] Go home\n[2] Find a store\n[3] Search the area");
choice = keyboard.nextInt();
if (choice == 1){
System.out.println("\nYou head home with " + dogName + " as fast as you can.\n"
+"On the way back, " + dogName + " seems extremely happy to be with"
+ " his new owner.\nGoing out you had no idea you'd bring home a new friend.");
karma++;
}
if (choice == 2){
System.out.println("");
System.out.println("");
}
if (choice == 3){
}
}
// GAME ENDING
if (karma > 0) {
System.out.println("\nYou ended with " + karma + " karma. Good job!");
}
else if (karma == 0){
System.out.println("\nYou ended with " + karma + " karma. Neither good nor bad, a neutral state.");
}else{
System.out.println("\nYou ended with " + karma + " karma. Bad job!");
}
// CREDITS
System.out.println("\n\t# THANK YOU FOR PLAYING #");
System.out.println("\t# Game created by aliens from outer space #");
}
}有两件事
private String dogName;
public Dog(String dogName)
this.dogName = dogName;
}
//getters setters..
}
private String dogName;
public Dog(String dogName)
this.dogName = dogName;
}
//getters setters..
}
您不必将所有内容都放在main中,而是必须删除static关键字,并创建一个新的程序实例,如下所示:
public static void main(String[] args) {
Game game = new Game();
game.start();
}
然后选项
和dogName
变量不必再是静态的
一般说明:首先,将代码拆分为多个方法,每个方法按功能分组,例如,一个用于处理用户输入的方法,一个用于打印选项的方法,等等。这样,您的代码就不会那么混乱,并允许您以后更容易地重构为不同的类。而不是将所有内容都放在一起在main中,您必须删除static关键字,并创建程序的新实例,如下所示:
public static void main(String[] args) {
Game game = new Game();
game.start();
}
然后选项
和dogName
变量不必再是静态的
一般说明:首先,将代码拆分为多个方法,每个方法按功能分组,例如,一个用于处理用户输入的方法,一个用于打印选项的方法等。这样,您的代码就不会那么混乱,并允许您以后更容易地重构为不同的类。涵盖了您的实际错误,但t我想我应该在“你能做得更好”上加上更多
请注意所有这些字段是如何静态的?这是必需的,因为您正试图从静态
方法(该方法为main
)使用它们。仅使用您发布的代码,您似乎可以将这些字段移动到main
方法中,就像创建Dog
一样
此外,我的一个小小的恼怒,但它实际上不是一个规则或任何东西,是“过度”的间隔(比如选择和狗名)。一个空格很好。两个空格对我来说太多了
Grr,更多空格!但更重要的是关于狗丹迪
的那一行。你的变量名是大写的,最好用小写字母命名变量(比如你对卡玛
做得正确)。你也应该在同一行声明和初始化你的狗
,就像这样:
狗丹迪=新狗();
使用<代码>序言和章节 ,可以考虑将这些分隔成一个单独的类。您可能注意到每章中的一个模式。
给出了一些文本
给出了选项
将显示选项的结果
你可以通过创建一个类来极大地提高代码的整体可读性,这个类可能需要一些简介
,选项
,然后根据所做的选择来显示选项
。如果这似乎让你难以理解,那么我就不必担心了-这只是你的第三周,所以我不会花费太多时间让你对类
、方法
和字段
之间的差异有一个清晰的认识。如果这是你非常感兴趣的东西,你可以找到涵盖这些内容的各种教程,当你真正了解它们可以做什么以及它们如何交互时,这将非常有益。涵盖你的实际错误,但我想我应该在“你能做得更好”上加上更多
请注意所有这些字段是如何静态的?这是必需的,因为您正试图从静态
方法(该方法为main
)使用它们。仅使用您发布的代码,您似乎可以将这些字段移动到main
方法中,就像创建Dog
一样
此外,我的一个小小的恼怒,但它实际上不是一个规则或任何东西,是“过度”的间隔(比如选择和狗名)。一个空格很好。两个空格对我来说太多了
Grr,更多空格!但更重要的是关于狗丹迪
的那一行。你的变量名是大写的,最好用小写字母命名变量(比如你对卡玛
做得正确)。你也应该在同一行声明和初始化你的狗
,就像这样:
狗丹迪=新狗();
使用<代码>序言和章节 ,可以考虑将这些分隔成一个单独的类。您可能注意到每章中的一个模式。
给出了一些文本
给出了选项
将显示选项的结果
你可以通过创建一个类来极大地提高代码的整体可读性,这个类可能需要一些简介
,选项
,然后根据所做的选择来显示选项
。如果这似乎让你难以理解,那么我就不必担心了-这只是你的第三周,所以我不会花费太多时间您希望了解类
、方法
和字段
之间的差异。如果这是一些
case 2:
keyboard.nextLine();
dogName = keyboard.nextLine();
karma++;