繁体   English   中英

如何防止我的计数器重置为 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); 

这样,程序看到的唯一feedCounterplayCounter等是static的。

发生这种情况是因为 Java 是按值传递的:调用方法时,参数的值被复制到被调用方法的参数列表中的局部变量中。

bunnyName似乎不会发生这种情况。 那是因为bunnyName是一个String ,它是一个引用类型。 计数器是原语。

对于引用类型,传递给被调用方法的是引用的副本。 被调用方法中的局部变量成为执行调用的相应变量的别名。 在被调用方法中,只要局部引用变量没有更改为指向其他内容,在被调用方法中发生的事情也会在调用方法中发生。

题外话:当你在做的时候,在其中添加static Scanner in; static变量列表。 main的开头,有in = new Scanner (System.in); . 这样,您就不必在其他方法中打开new Scanner (System.in)

要了解更多信息,请搜索“Java 变量阴影”和“Java 按值调用”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM