只有我上课的第三周(编程新手)。 我用Java制作了一个基于文本的故事,但我在这个过程中遇到了一个问题。我有一个静态变量叫做#34; static String dogName;"我试图改变(只有一次)的价值。在游戏开始时,用户可以选择为他们的狗命名。当我尝试命名狗时,由于静态String dogName,代码会跳过命名提示符。
部分代码可能不像决定那样完整......
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 #");
}
}
答案 0 :(得分:2)
当我尝试命名狗时,由于静态String dogName,代码会跳过命名提示符。
不,问题与dogName
静态无关。相反,问题在于您使用Scanner
的方式。执行keyboard.nextInt()
后,它会读取足够的数据,以便能够返回int
。因此,如果用户键入2并输入,则扫描程序将读取2并将其作为int
返回,将换行符保留在输入缓冲区中。
然后,当您使用dogName = keyboard.nextLine();
读取狗的名字时,已经存在的换行符会立即返回狗名称的空字符串,而不是等待任何用户输入。
你可以在你要求狗的名字之前做另一个keyboard.nextLine()
来解决这个问题:
case 2:
keyboard.nextLine();
dogName = keyboard.nextLine();
karma++;
第一个nextLine()
从输入的上一个数字中删除换行符,第二个nextLine()
返回可以分配给dogName
的一行文本(无换行符)。
但是,Scanner
会遇到其他问题。如果玩家输入的数字不是数字,nextInt()
会抛出InputMismatchException
。可以解决这些问题,但最终会让你头疼。
最好每次使用keyboard.nextLine()
从播放器获取一行,然后检查它是否包含一个数字并解析该数字。
此外,Java中的约定是使用小写字母开始变量名称,因此您的变量Dandy
应该命名为dandy
。其他人已经提出了一些合理的建议,即将程序拆分成碎片,而不是采用单一的main
方法。
答案 1 :(得分:1)
为Dog创建一个POJO(我认为你已经有一个)类,它在构造函数参数中取名。
公共班级狗{
private String dogName;
public Dog(String dogName)
this.dogName = dogName;
}
//getters setters..
}
答案 2 :(得分:1)
您不必将所有内容都放在main中,而是必须删除static关键字,并创建一个新的程序实例,如下所示:
public static void main(String[] args) {
Game game = new Game();
game.start();
}
然后choice
和dogName
变量不再是静态的。
一般说明:首先将代码拆分为多个方法,每个方法按功能分组,例如,一个用于处理用户输入的方法,一个用于打印选项的方法等。这样您的代码将变得不那么简单混乱,并允许您以后更轻松地重构为不同的类。
答案 3 :(得分:0)
@DavidConrad's answer涵盖了您的实际错误,但我认为我会更多地添加"您可以做得更好"
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;
注意所有这些字段是如何静态的?这只是必需的,因为您尝试使用static
方法(该方法为main
)来使用它们。只使用您发布的代码,您似乎可以将这些字段移到main
方法中,就像您创建Dog
的位置一样。
此外,我的一个小小的烦恼,但它实际上不是一个规则或任何东西,是"过度"间距 - (如inbetween choice
和dogName
)。一个空间很好。两个也是#34;"很多人都喜欢。 ;)
public static void main(String[] args) {
int karma = 0;
// Dog stuff...
Dog Dandy;
Dandy = new Dog();
Grr,更多空间!但更重要的是关于Dog Dandy
的界限。您的变量名称是大写的,最好将变量命名为小写(,例如您为karma
正确执行的操作)。您还应该在同一行中声明并初始化Dog
,例如:Dog dandy = new Dog();
使用prologue
和chapters
,您可以考虑将它们分成单独的类。您可能会注意到每章中的模式。
您可以通过创建一个可能需要introText
,options
,然后根据option
显示choice
的类来极大地提高代码的整体可读性。制作。如果这似乎超出了你的想法,那么我不会担心它 - 它只是你的第三周,所以我不希望你有classes
,{{methods
之间的差异1}},而且fields
还没下来。如果这是你非常感兴趣的东西,你可以找到涵盖这些内容的所有类型的教程,当你真正理解他们可以做什么以及他们如何互动时,这将是非常有益的。