我正在开发一个项目,用户输入当前日期和生日。整个代码计算他们的年龄以及打印日期。对于当前日期,它应该显示2016年11月14日,但显示0/14/2016,我无法弄清楚我哪里出错了。
以下是代码:
public class BroDoYouEvenAge {
// add in try-catch statements x
// only numeric values x
// leap year
// loop if invalid
// loop if day is invalid
// less than 1 or greater than max day x
// add in no spaces or letters x
// VALIDATION FOR MONTHS x
// loop if invalid
// same year x
// try again x
// loop if invalid
static int cDay;
static int cMonth;
static int bYear;
static int bMonth;
static int bDay;
public static void run() {
prompt();
retry();
}
public static void prompt() {
BufferedReader delta = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Please enter the current year");
String Year = delta.readLine();
int cYear = Integer.parseInt(Year);
// prompts the user for the current month as a numeric value
boolean validmonth = false;
while(!validmonth){
System.out.println("Please enter the current month as a numeric value");
String Month = delta.readLine();
int cMonth = Integer.parseInt(Month);
if (cMonth < 1 || cMonth > 12) {
System.out.println("Your input was invalid");
}else{
validmonth = true;
}
}
// prompts the user for the current day as a numeric value
boolean validday = false;
while (!validday) {
System.out.println("Please enter the current day as a numeric value");
String Day = delta.readLine();
cDay = Integer.parseInt(Day);
if (cDay < 1) {
System.out.println("Your input was invalid");
}else if (cDay > 31) {
System.out.println("Your input was invalid");
}else if (cMonth == 4 && cDay > 30) {
System.out.println("Your answer is invalid");
}else if (cMonth == 6 && cDay > 30) {
System.out.println("Your answer is invalid");
}else if (cMonth == 9 && cDay > 30) {
System.out.println("Your answer is invalid");
}else if (cMonth == 11 && cDay > 30) {
System.out.println("Your answer is invalid");
}else{
validday = true;
}
}
// prompts the user for their birth year
boolean validYear = false;
while(!validYear){
System.out.println("Please enter your birth year");
String Year2 = delta.readLine();
bYear = Integer.parseInt(Year2);
if (bYear > cYear) {
System.out.println("Your answer was invalid");
}else{
validYear = true;
}
}
// does not allow user to input a value greater than their current
// year
// loops if invalid
// prompts the user for their birth month
boolean _validmonth = false;
while(!_validmonth){
System.out.println("Please enter your birth month as a numeric value");
String Month2 = delta.readLine();
bMonth = Integer.parseInt(Month2);
if(bMonth < 1 || bMonth> 12){
System.out.println("Your answer was invalid");
}else{
_validmonth = true;
}
}
// prompts the user for their birth day
boolean _validday = false;
while(!_validday){
System.out.println("Please enter the day you were born as a numeric value");
String Day2 = delta.readLine();
bDay = Integer.parseInt(Day2);
// prints out collected data
if (bMonth == 4 && bDay > 30) {
System.out.println("Your answer is invalid");
}else if (bMonth == 6 && bDay > 30) {
System.out.println("Your answer is invalid");
}else if (bMonth == 9 && bDay > 30) {
System.out.println("Your answer is invalid");
}else if (bMonth == 11 && bDay > 30) {
System.out.println("Your answer is invalid");
}else{
_validday= true;
}
}
System.out.println("The current date is " + cMonth + "/" + cDay + "/" + cYear);
System.out.println("Your birthday is " + bMonth + "/" + bDay + "/" + bYear);
// calculates and prints age
if (cMonth > bMonth) {
int age = cYear - bYear;
System.out.println("You are " + age + " years old ");
} else if (cMonth < bMonth) {
int age = (cYear - bYear);
System.out.println("You are " + age + " years old ");
} else if (cMonth == bMonth && cDay > bDay) {
int age = cYear - bYear;
System.out.println("You are " + age + " years old.");
} else if (cMonth == bMonth && cDay < bDay) {
int age = (cYear - bYear) - 1;
System.out.println("You are " + age + " years old. ");
} else if (cYear == bYear) {
System.out.println("You are not even a year old.");
} else {
System.out.println("Your input was invalid");
}
} catch (IOException IOE) {
} catch (NumberFormatException NFE) {
System.out.println("Please enter a numeric value without spaces, letters, or special characters");
}
}
public static void retry() {
BufferedReader delta = new BufferedReader(new InputStreamReader(System.in));
try {
boolean valid = false;
while (!valid) {
System.out.println("Would you like to try again? 1) Yes or 2) No");
System.out.println("Please enter a number");
String retry = delta.readLine();
int trynum = Integer.parseInt(retry);
if (trynum < 1 || trynum > 2) {
System.out.println("Your input was invalid. Please enter 1 or 2.");
continue;
} else if (trynum == 1) {
prompt();
} else if (trynum == 2) {
System.out.println("Okay then.");
System.exit(0);
} else {
System.out.println("Invalid");
continue;
}
}
} catch (IOException IOE) {
} catch (NumberFormatException NFE) {
System.out.println("Please enter a number");
}
}
}
答案 0 :(得分:0)
在读取行
上的月份值时,您正在初始化新的cMonth变量System.out.println("Please enter the current month as a numeric value");
String Month = delta.readLine();
int cMonth = Integer.parseInt(Month);
只需删除cMonth局部变量的初始化,就像要引用全局变量一样。
System.out.println("Please enter the current month as a numeric value");
String Month = delta.readLine();
cMonth = Integer.parseInt(Month);
为您提供我正在粘贴工作解决方案的工作源代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BroDoYouEvenAge {
// add in try-catch statements x
// only numeric values x
// leap year
// loop if invalid
// loop if day is invalid
// less than 1 or greater than max day x
// add in no spaces or letters x
// VALIDATION FOR MONTHS x
// loop if invalid
// same year x
// try again x
// loop if invalid
static int cDay;
static int cMonth;
static int bYear;
static int bMonth;
static int bDay;
public static void main(String[] args) {
run();
}
public static void run() {
prompt();
retry();
}
public static void prompt() {
BufferedReader delta = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Please enter the current year");
String Year = delta.readLine();
int cYear = Integer.parseInt(Year);
// prompts the user for the current month as a numeric value
boolean validmonth = false;
while(!validmonth) {
System.out.println("Please enter the current month as a numeric value");
String Month = delta.readLine();
cMonth = Integer.parseInt(Month);
if(cMonth < 1 || cMonth > 12) {
System.out.println("Your input was invalid");
} else {
validmonth = true;
}
}
// prompts the user for the current day as a numeric value
boolean validday = false;
while(!validday) {
System.out.println("Please enter the current day as a numeric value");
String Day = delta.readLine();
cDay = Integer.parseInt(Day);
if(cDay < 1) {
System.out.println("Your input was invalid");
} else if(cDay > 31) {
System.out.println("Your input was invalid");
} else if(cMonth == 4 && cDay > 30) {
System.out.println("Your answer is invalid");
} else if(cMonth == 6 && cDay > 30) {
System.out.println("Your answer is invalid");
} else if(cMonth == 9 && cDay > 30) {
System.out.println("Your answer is invalid");
} else if(cMonth == 11 && cDay > 30) {
System.out.println("Your answer is invalid");
} else {
validday = true;
}
}
// prompts the user for their birth year
boolean validYear = false;
while(!validYear) {
System.out.println("Please enter your birth year");
String Year2 = delta.readLine();
bYear = Integer.parseInt(Year2);
if(bYear > cYear) {
System.out.println("Your answer was invalid");
} else {
validYear = true;
}
}
// does not allow user to input a value greater than their current
// year
// loops if invalid
// prompts the user for their birth month
boolean _validmonth = false;
while(!_validmonth) {
System.out.println("Please enter your birth month as a numeric value");
String Month2 = delta.readLine();
bMonth = Integer.parseInt(Month2);
if(bMonth < 1 || bMonth > 12) {
System.out.println("Your answer was invalid");
} else {
_validmonth = true;
}
}
// prompts the user for their birth day
boolean _validday = false;
while(!_validday) {
System.out.println("Please enter the day you were born as a numeric value");
String Day2 = delta.readLine();
bDay = Integer.parseInt(Day2);
// prints out collected data
if(bMonth == 4 && bDay > 30) {
System.out.println("Your answer is invalid");
} else if(bMonth == 6 && bDay > 30) {
System.out.println("Your answer is invalid");
} else if(bMonth == 9 && bDay > 30) {
System.out.println("Your answer is invalid");
} else if(bMonth == 11 && bDay > 30) {
System.out.println("Your answer is invalid");
} else {
_validday = true;
}
}
System.out.println("The current date is " + cMonth + "/" + cDay + "/" + cYear);
System.out.println("Your birthday is " + bMonth + "/" + bDay + "/" + bYear);
// calculates and prints age
if(cMonth > bMonth) {
int age = cYear - bYear;
System.out.println("You are " + age + " years old ");
} else if(cMonth < bMonth) {
int age = cYear - bYear;
System.out.println("You are " + age + " years old ");
} else if(cMonth == bMonth && cDay > bDay) {
int age = cYear - bYear;
System.out.println("You are " + age + " years old.");
} else if(cMonth == bMonth && cDay < bDay) {
int age = cYear - bYear - 1;
System.out.println("You are " + age + " years old. ");
} else if(cYear == bYear) {
System.out.println("You are not even a year old.");
} else {
System.out.println("Your input was invalid");
}
} catch(IOException IOE) {
} catch(NumberFormatException NFE) {
System.out.println("Please enter a numeric value without spaces, letters, or special characters");
}
}
public static void retry() {
BufferedReader delta = new BufferedReader(new InputStreamReader(System.in));
try {
boolean valid = false;
while(!valid) {
System.out.println("Would you like to try again? 1) Yes or 2) No");
System.out.println("Please enter a number");
String retry = delta.readLine();
int trynum = Integer.parseInt(retry);
if(trynum < 1 || trynum > 2) {
System.out.println("Your input was invalid. Please enter 1 or 2.");
continue;
} else if(trynum == 1) {
prompt();
} else if(trynum == 2) {
System.out.println("Okay then.");
System.exit(0);
} else {
System.out.println("Invalid");
continue;
}
}
} catch(IOException IOE) {
} catch(NumberFormatException NFE) {
System.out.println("Please enter a number");
}
}
}