我正在尝试用java编写一个程序,它将输入当前日期和用户生日的日期,然后输出用户下一个生日的天数。
在尝试编译我的代码时,我收到一个"找不到符号变量absolutepday"错误。我已多次查看我的代码,无法弄清楚出了什么问题。 (我相信这与我的变量范围有关,但我不确定)(我是初学者)
如何修复它以便这些变量包含在我的范围内?
非常感谢任何帮助和建议!
这是我的代码:
import java.util.Scanner;
public class BirthdayProject
{
//programs purpose
public static void initialstatement()
{
System.out.println("This program tells you how many days\nit will be until your next birthday.");
System.out.println();
}
//calculate absolute day of the year of todays date
public static int todaysdate()
{
int absolutepday = 0;
Scanner input = new Scanner(System.in);
System.out.println("Please enter today's date:");
System.out.print("What is the month (1-12)? ");
int pmonth= input.nextInt();
System.out.print("What is the day (1-31)? ");
int pday= input.nextInt();
if (pmonth == 1)
absolutepday = pday;
else if (pmonth == 2)
absolutepday = 31 + pday;
else if (pmonth == 3)
absolutepday = 59 + pday;
else if (pmonth == 4)
absolutepday = 90 + pday;
else if (pmonth == 5)
absolutepday = 120 + pday;
else if (pmonth == 6)
absolutepday = 151 + pday;
else if (pmonth == 7)
absolutepday = 181 + pday;
else if (pmonth == 8)
absolutepday = 212 + pday;
else if (pmonth == 9)
absolutepday = 243 + pday;
else if (pmonth == 10)
absolutepday = 273 + pday;
else if (pmonth == 11)
absolutepday = 304 + pday;
else if (pmonth == 12)
absolutepday = 334 + pday;
System.out.println(pmonth + "/" + pday + " is day #" + absolutepday + " of 365");
System.out.println();
return absolutepday;
}
//calculate absolute day of the year of the users birthday
public static int birthdaydate()
{
int absolutebday = 0;
Scanner input = new Scanner(System.in);
System.out.println("Please enter your birthday:");
System.out.print("What is the month (1-12)? ");
int bimonth= input.nextInt();
System.out.print("What is the day (1-30)? ");
int biday= input.nextInt();
if (bimonth == 1)
absolutebday = biday;
else if (bimonth == 2)
absolutebday = 31 + biday;
else if (bimonth == 3)
absolutebday = 59 + biday;
else if (bimonth == 4)
absolutebday = 90 + biday;
else if (bimonth == 5)
absolutebday = 120 + biday;
else if (bimonth == 6)
absolutebday = 151 + biday;
else if (bimonth == 7)
absolutebday = 181 + biday;
else if (bimonth == 8)
absolutebday = 212 + biday;
else if (bimonth == 9)
absolutebday = 243 + biday;
else if (bimonth == 10)
absolutebday = 273 + biday;
else if (bimonth == 11)
absolutebday = 304 + biday;
else if (bimonth == 12)
absolutebday = 334 + biday;
System.out.println(bimonth + "/" + biday +" is day #" + absolutebday + " of 365.");
System.out.println();
return absolutebday;
}
//calculate how many days intill the users next birthday
public static void main()
{
initialstatement();
todaysdate();
birthdaydate();
int absolutedaystillbday = (absolutepday-absolutebday)+(absolutepmonth-absolutebmonth);
if (absolutedaystillbday == 0)
System.out.println("Happy Birthday!");
else if (absolutedaystillbday == 1)
System.out.println("Wow, your birthday is tomorrow!");
else if (absolutedaystillbday > 0)
System.out.println("Your next birthday is in " + absolutedaystillbday + "days.");
else if (absolutedaystillbday < 0)
System.out.print("Your next birthday is in " + (absolutedaystillbirthday + 365) + "days.");
}
}
&#13;