我正在创建一个类似于宠物兔子模拟器的程序。在程序中,你将面临四天(这几天每一天的一种方法)而且在这几天里你被要求做决定,比如我的程序会问你是想喂、宠、还是和兔子玩,如果用户键入“喂”,将调用一个方法,该方法添加到一个计数器,该计数器记录用户喂兔子的次数。我认为正在发生的事情是在我的代码中的某个地方,计数器不断重置回0。尽管我不确定在哪里,也不知道该怎么解决。
import java.io.*;
import java.util.*;
public class BunnyProgram
{
static int playCounter;
static int feedCounter;
static int cleanLitterCounter;
static int treatCounter;
static int petCounter;
static int loveCounter;
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
welcomeStatement();
String bunnyName = nameYourBun();
DayOne(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
DayOneChoice(bunnyName, loveCounter);
results(bunnyName, playCounter, feedCounter, cleanLitterCounter, treatCounter, petCounter, loveCounter);
DayTwo(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
DayTwoChoice(bunnyName, loveCounter);
DayThree(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
DayThreeChoice(bunnyName, loveCounter);
DayFour(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
DayFourChoice(bunnyName, loveCounter);
results(bunnyName, playCounter, feedCounter, cleanLitterCounter, treatCounter, petCounter, loveCounter);
}
public static void welcomeStatement()
{
System.out.println("Hiya!! Welcome to the bunny program. In this program, you'll receive a pet bunny!");
System.out.println("It's your job to make correct choices over the span of four days and prevent your bunny from running away!");
System.out.println("It is highly recommended to complete every possible action for every day.");
}
public static String nameYourBun()
{
Scanner in;
in = new Scanner(System.in);
String bunnyName;
System.out.println("\nPlease enter a name for your bunny!");
bunnyName = in.nextLine();
System.out.println("Aww, so cute! Welcome to the (digital) world, " + bunnyName + "!!");
System.out.println("Let's get started! :)");
return bunnyName;
}
public static void DayOne(String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
{
Scanner in;
in = new Scanner(System.in);
String answer = null;
System.out.println("\nDay One");
System.out.println("_________\n");
boolean runIt = true;
while (runIt)
{
chooseYourAction(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
System.out.println("Would you like to complete another action? Yes or no");
answer = in.nextLine();
if(answer.charAt(0) == 'N' || answer.charAt(0) == 'n')
{
runIt = false;
}
}
}
public static void DayOneChoice(String bunnyName, int loveCounter)
{
Scanner in;
in = new Scanner(System.in);
String userAnswer;
int Situation;
Situation = (int) (Math.random() * 3) + 1;
if (Situation == 1)
{
System.out.println("You watch " + bunnyName + " look longily outside.");
System.out.println("Do you let " + bunnyName + " outside? Yes or no");
userAnswer = in.nextLine();
if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
{
loveCounter = +1;
}
if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
{
loveCounter = -1;
}
}
if (Situation == 2)
{
System.out.println(bunnyName + " looks outside with no particular interest or disinterest.");
System.out.println("Do you let " + bunnyName + " outside? Yes or no");
userAnswer = in.nextLine();
if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
{
loveCounter += 0;
}
if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
{
loveCounter += 0;
}
}
if (Situation == 3)
{
System.out.println("You see " + bunnyName + " look outside with immense fear.");
System.out.println("Do you let " + bunnyName + " outside? Yes or no");
userAnswer = in.nextLine();
if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
{
loveCounter += -1;
}
if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
{
loveCounter += 1;
}
}
System.out.println("Okay! Let's go to the next day!");
}
public static void DayTwo(String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
{
Scanner in;
in = new Scanner(System.in);
String answer = null;
System.out.println("\nDay Two");
System.out.println("_________\n");
boolean runIt = true;
while (runIt)
{
chooseYourAction(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
System.out.println("Would you like to complete another action? Yes or no");
answer = in.nextLine();
if(answer.charAt(0) == 'N' || answer.charAt(0) == 'n')
{
runIt = false;
}
}
}
public static void DayTwoChoice(String bunnyName, int loveCounter)
{
Scanner in;
in = new Scanner(System.in);
String userAnswer;
System.out.println(bunnyName + " has continuously been staring over your shoulder at an ad for a new toy.");
System.out.println("Do you buy " + bunnyName + " the toy? Choose option 1, 2, or 3.\n");
System.out.println("1. Buy " + bunnyName + " the toy");
System.out.println("2. Buy " + bunnyName + " a cheaper toy");
System.out.println("3. Don't buy " + bunnyName + " any toys");
int num;
num = in.nextInt();
switch(num)
{
case 1:
loveCounter += 1;
case 2:
loveCounter += 0;
case 3:
loveCounter += -1;
}
System.out.println("Okay! Let's go to the next day!");
}
public static void DayThree(String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
{
Scanner in;
in = new Scanner(System.in);
String answer = null;
System.out.println("\nDay Three");
System.out.println("_________\n");
boolean runIt = true;
while (runIt)
{
chooseYourAction(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
System.out.println("Would you like to complete another action? Yes or no");
answer = in.nextLine();
if(answer.charAt(0) == 'N' || answer.charAt(0) == 'n')
{
runIt = false;
}
}
}
public static void DayThreeChoice(String bunnyName, int loveCounter)
{
Scanner in;
in = new Scanner(System.in);
String userAnswer;
int Situation;
Situation = (int) (Math.random() * 3) + 1;
if (Situation == 1)
{
System.out.println(bunnyName + " has been jumping onto your bed a lot recently.");
System.out.println("Do you set " + bunnyName + " down on your bed to sleep next to you at night? Yes or no");
userAnswer = in.nextLine();
if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
{
loveCounter = +1;
}
if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
{
loveCounter = -1;
}
}
if (Situation == 2)
{
System.out.println("You catch " + bunnyName + " sleeping on your bed sometimes, and in their own bed other times.");
System.out.println("Do you set " + bunnyName + " down on your bed to sleep next to you at night? Yes or no");
userAnswer = in.nextLine();
if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
{
loveCounter += 0;
}
if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
{
loveCounter += 0;
}
}
if (Situation == 3)
{
System.out.println(bunnyName + " absolutely loves their bed and seems the most comfortable there.");
System.out.println("Do you set " + bunnyName + " down on your bed to sleep next to you at night? Yes or no");
userAnswer = in.nextLine();
if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
{
loveCounter += -1;
}
if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
{
loveCounter += 1;
}
}
System.out.println("Okay! Let's go to the final day!");
}
public static void DayFour(String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
{
Scanner in;
in = new Scanner(System.in);
String answer = null;
System.out.println("\nDay Four");
System.out.println("_________\n");
boolean runIt = true;
while (runIt)
{
chooseYourAction(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
System.out.println("Would you like to complete another action? Yes or no");
answer = in.nextLine();
if(answer.charAt(0) == 'N' || answer.charAt(0) == 'n')
{
runIt = false;
}
}
}
public static void DayFourChoice(String bunnyName, int loveCounter)
{
Scanner in;
in = new Scanner(System.in);
String userAnswer;
System.out.println("You notice " + bunnyName + " has been limping.");
System.out.println("How do you treat " + bunnyName + "? Choose option 1, 2, or 3.\n");
System.out.println("1. Take " + bunnyName + " to the vet");
System.out.println("2. Try and take care of " + bunnyName + "'s paw by yourself.");
System.out.println("3. Ignore " + bunnyName + "'s paw");
int num;
num = in.nextInt();
switch(num)
{
case 1:
loveCounter += 1;
case 2:
loveCounter += 0;
case 3:
loveCounter += 1;
}
System.out.println("Okay! Let's see how you did!");
}
public static void chooseYourAction(String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
{
Scanner in;
in = new Scanner(System.in);
String choice;
System.out.println("Would you like to feed " + bunnyName + ", pet " + bunnyName + ", play with ");
System.out.println(bunnyName + ", clean " + bunnyName + "'s litter, or give " + bunnyName + " a treat?");
choice = in.nextLine();
YourChoice(choice, bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
}
public static void YourChoice (String choice, String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
{
switch (choice.charAt(0))
{
case 'F':
case 'f':
{
feedCounter = feedBunny(bunnyName, feedCounter);
break;
}
}
switch (choice.charAt(0))
{
case 'C':
case 'c':
case 'L':
case 'l':
{
cleanLitterCounter = cleanLitter(bunnyName, cleanLitterCounter);
break;
}
}
switch (choice.charAt(0))
{
case 'G':
case 'g':
case 'T':
case 't':
{
treatCounter = giveTreat(bunnyName, treatCounter);
break;
}
}
switch (choice.charAt(0))
{
case 'P':
case 'p':
{
switch (choice.charAt(1))
{
case 'E':
case 'e':
{
petCounter = petBunny(bunnyName, petCounter);
break;
}
case 'L':
case 'l':
{
playCounter = playWithBunny(bunnyName, playCounter);
break;
}
}
}
}
}
public static int feedBunny(String bunnyName, int feedCounter)
{
Scanner in;
in = new Scanner(System.in);
System.out.println("How many times will you feed " + bunnyName + " today?");
feedCounter += in.nextInt();
if (feedCounter == 1)
{
feedCounter += 0;
}
if(feedCounter == 2)
{
feedCounter += 1;
}
if(feedCounter == 0)
{
feedCounter += -1;
}
if(feedCounter > 2)
{
feedCounter += -2;
}
return feedCounter;
}
public static int cleanLitter(String bunnyName, int cleanLitterCounter)
{
Scanner in;
in = new Scanner(System.in);
System.out.println("How many times will you clean " + bunnyName + "'s litter today?");
cleanLitterCounter += in.nextInt();
if (cleanLitterCounter == 1)
{
cleanLitterCounter += 1;
}
if(cleanLitterCounter == 2 || cleanLitterCounter == 0)
{
cleanLitterCounter += -1;
}
return cleanLitterCounter;
}
public static int giveTreat(String bunnyName, int treatCounter)
{
Scanner in;
in = new Scanner(System.in);
System.out.println("How many times will you give " + bunnyName + " a treat today?");
treatCounter += in.nextInt();
if (treatCounter == 1)
{
treatCounter += 1;
}
if(treatCounter == 2)
{
treatCounter += -1;
}
if(treatCounter == 0)
{
treatCounter += 0;
}
if(treatCounter > 2)
{
treatCounter += -2;
}
return treatCounter;
}
public static int petBunny(String bunnyName, int petCounter)
{
Scanner in;
in = new Scanner(System.in);
System.out.println("How many times will you pet " + bunnyName + " today?");
petCounter += in.nextInt();
if (petCounter == 1)
{
petCounter += 0;
}
if(petCounter == 2)
{
petCounter += 1;
}
if(petCounter == 0 || petCounter > 2)
{
petCounter += -1;
}
return petCounter;
}
public static int playWithBunny(String bunnyName, int playCounter)
{
Scanner in;
in = new Scanner(System.in);
System.out.println("How many times will you play with " + bunnyName + " today?");
playCounter += in.nextInt();
if (playCounter == 1)
{
playCounter += 1;
}
if(playCounter == 2)
{
playCounter += 2;
}
if(playCounter == 0)
{
playCounter += -1;
}
return playCounter;
}
public static int loveTotal (int playCounter, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int loveCounter)
{
loveCounter = playCounter + feedCounter + cleanLitterCounter + treatCounter + petCounter;
return loveCounter;
}
public static void results (String bunnyName, int playCounter, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int loveCounter)
{
int lC = loveTotal(playCounter, feedCounter, cleanLitterCounter, treatCounter, petCounter, loveCounter);
System.out.println(lC);
if(lC >= 18)
{
System.out.println(bunnyName + " comes up to you to give you a million kisses!");
System.out.println("You're an amazing bunny parent! <3");
}
if(lC <= 17 && lC >= 0)
{
System.out.println(bunnyName + " looks at you with a blank expression and hops the other way.");
System.out.println("You're an alright bunny parent.. but more effort would be appreciated.");
}
if(lC < 0)
{
System.out.println("You wake up one morning to find " + bunnyName + "'s area vacant.");
System.out.println("Maybe if you would have been a better bunny parent things would have turned out differently..");
}
}
}
最后,我打印了我的结果,但它不会停止打印0。我不知道该怎么做,我觉得我所尝试的一切都不起作用。我在公共类中初始化了我的变量,但这没有任何帮助。我还将每个变量设置为“+=”,所以我不知道为什么每次调用方法时它不添加值。
1条答案
按热度按时间8zzbczxx1#
请看下面几行:
这个方法的开始:
您的错误是将
int
计数器作为参数传递。在方法DayOne
中,变量feedCounter
与static void main
上面声明的static int feedCounter
是一个 * 不同 * 的变量。两个变量具有相同的名称,但局部变量隐藏了静态变量。这同样适用于传递的其他int
变量。因此,当在该方法内改变它们中的一个的值时,在main
内不改变。若要解决此问题,请不要通过它们:
并且在
main
中这样,程序只能看到
feedCounter
、playCounter
等是static
。这是因为Java是按值传递的:调用方法时,参数的值被复制到被调用方法的参数列表中的局部变量。
bunnyName
似乎不会发生这种情况。这是因为bunnyName
是String
,它是一个引用类型。计数器是原语。对于引用类型,传递给被调用方法的是引用的副本。被调用方法中的局部变量成为相应变量的别名。在被调用方法中,只要局部引用变量没有被更改为指向其他变量,在被调用方法中发生的事情也会在调用方法中发生。
离题:当你这样做的时候,把
static Scanner in;
添加到static
变量列表中。在main
的开头,有in = new Scanner (System.in);
。这样,你就不必在其他方法中打开一个new Scanner (System.in)
。要了解更多信息,请搜索“Java变量阴影”和“Java按值调用”