Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Counting Days problem with object

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Counting Days problem with object

    Hi! I'm current working on a game that would be like a harvest moon game. But right now I'm having a problem on how to be able to count the day per end of each turn. Here's the sample code. Sorry but I'm still a newbie on Java Programming.

    public class GameMenu {
     
    	public void Main()	{
     
    		FarmMap map = new FarmMap();
    		Calender date = new Calender();
    		GameMenu r = new GameMenu();
     
    		Scanner keyIn = new Scanner(System.in);
     
    		System.out.println("--------------------------------");
    		System.out.println("|| [1] Go to Farm             ||");
    		System.out.println("|| [2] Go to Shop             ||");
    		System.out.println("|| [3] End the day            ||");
    		System.out.println("--------------------------------");
    		System.out.println("\nWhat would you like to do? ");
    		int ans1 = keyIn.nextInt();
     
    		if(ans1 == 1)
    			map.FarmMap1();
     
    		if(ans1 == 3)	{
     
    			date.Spring();
     
     
    		}
     
    	}
     
    }

    public class Calender {
     
    	GameMenu m = new GameMenu();
    	public void Spring()	{
     
    		int Day = 0;
    		Day++;
     
    		System.out.println("Day "+Day+", Spring");
    		m.Main();
     
    		if(Day == 30)	{
     
    		System.out.println("It's Summer Time!");
     
    		}
    	}

    Here is the outcome...

    Welcome to the farm game!
     
    --------------------------------
    || [1] Go to Farm             ||
    || [2] Go to Shop             ||
    || [3] End the day            ||
    --------------------------------
     
    What would you like to do? 
    3
    Day 1, Spring
    --------------------------------
    || [1] Go to Farm             ||
    || [2] Go to Shop             ||
    || [3] End the day            ||
    --------------------------------
     
    What would you like to do? 
    3
    Day 1, Spring
    --------------------------------
    || [1] Go to Farm             ||
    || [2] Go to Shop             ||
    || [3] End the day            ||
    --------------------------------
     
    What would you like to do? 
    3
    Day 1, Spring
    --------------------------------
    || [1] Go to Farm             ||
    || [2] Go to Shop             ||
    || [3] End the day            ||
    --------------------------------
     
    What would you like to do?

    I would really appreciate the help guys


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Counting Days problem with object

    Is there an error in the print out from the program? Can you explain what is wrong with the output and post what the output should be?

    One problem I see:
    	int Day = 0;
    	Day++;

    The variable: day (lower case for first letter) is defined inside of a method which means that it is local to that method and will go away when the method exits. If you want its value to be saved across method calls, move its definition out of any method to be a class variable.
    Last edited by Norm; September 6th, 2012 at 09:13 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Counting Days problem with object

    Whenever the user decides to end the day It should add 1 to the number of Day. Instead I keep getting "Day 1" whenever I choose to end the day. which is the default value. So where should i move my variable for "Day"?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Counting Days problem with object

    where should i move my variable for "Day"?
    Move the definition for day outside of any method. Next to where m is defined.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What's wrong with my code??? I have been struggling for 2 days !!!!
    By cuti in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 21st, 2012, 07:52 PM
  2. 12 days of xmas??? not showing
    By chonch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 09:45 PM
  3. Calculating Business days in java
    By narranil2 in forum Algorithms & Recursion
    Replies: 2
    Last Post: August 17th, 2010, 12:08 PM
  4. Getting date based on days past year
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 14th, 2010, 04:06 PM
  5. how do i get only the workig days for a certain month
    By anonimus83 in forum Algorithms & Recursion
    Replies: 2
    Last Post: January 11th, 2010, 11:13 AM