提问人:Sabrina 提问时间:12/19/2022 最后编辑:Sabrina 更新时间:12/19/2022 访问量:116
如何防止计数器重置回 0?
How Do I Prevent My Counter From Resetting Back To 0?
问:
我正在创建一个类似于宠物兔子模拟器的程序。在该计划中,您将面临四天(这些天中的每一天都有一种方法),在这些日子里,您被要求做出决定。例如,我的程序会询问你是否想喂兔子、抚摸兔子或与兔子玩耍。如果用户键入“feed”,则将调用一个方法,该方法将添加到计数器中,以跟踪用户喂兔子的次数。我认为正在发生的事情是在我的代码中的某个地方,计数器不断重置回 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。我不知道该怎么办,我觉得我尝试过的一切都没有奏效。我在公共类中初始化了我的变量,但这没有任何帮助。我还将每个变量设置为“+=”,所以我不知道为什么每次调用方法时它都没有添加值。
答:
请考虑以下几行:
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);
而这个方法的开始:
public static void DayOne
(String bunnyName, int feedCounter, int cleanLitterCounter
, int treatCounter, int petCounter, int playCounter)
{
您的错误是将计数器作为参数传递。在方法中,变量是与上面声明的变量不同的变量。两个变量具有相同的名称,但局部变量隐藏了静态变量。这同样适用于传递的其他变量。因此,当其中一个的值在方法中更改时,它不会在 中更改。int
DayOne
feedCounter
static int feedCounter
static void main
int
main
要解决此问题,请不要传递它们:
public static void DayOne (String bunnyName) {
并且,在main
DayOne (bunnyName);
这样,程序看到的唯一 、 等就是 那些。feedCounter
playCounter
static
发生这种情况是因为 Java 是按值传递的:调用方法时,参数的值被复制到被调用方法的参数列表中的局部变量中。
这似乎不会发生在 .那是因为 是 ,它是一种引用类型。计数器是基元。bunnyName
bunnyName
String
对于引用类型,传递给被调用方法的内容是引用的副本。被调用方法中的局部变量成为执行调用的相应变量的别名。在被调用方法中,只要局部引用变量不被更改为指向其他内容,则在被调用方法中发生的情况也会发生在调用方法中。
题外话:当你在做的时候,添加到变量列表中。在开头,有 .这样,您就不必在其他方法中打开。static Scanner in;
static
main
in = new Scanner (System.in);
new Scanner (System.in)
要了解更多信息,请搜索“Java 变量重影”和“Java 按值调用”
评论
DayOne
int
DayOne
DayOne
DayOne
Scanner
System.in
in
static