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.
Code :
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();
}
}
}
Code :
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...
Code :
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 :D
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:
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.
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"?
Re: Counting Days problem with object
Quote:
where should i move my variable for "Day"?
Move the definition for day outside of any method. Next to where m is defined.