【发布时间】:2015-01-05 14:02:12
【问题描述】:
我已经使用 BlueJ 使用 Java 编程 2 个月了,我需要一些帮助来完成作业。我正在制作一个基本的车辆购买数据输入程序。我需要从PurchaseDate 类中调用printPurchaseDate() 方法。我面临的问题是 print 语句具有三个 int 值:年、月和日。当我尝试在 Vehicle 类中的 printDetails() 方法中调用此方法时,它告诉我需要返回,如果我去掉 void,我必须将该方法标记为字符串。但是它不起作用,因为它在其中包含三个与 string 方法冲突的 int 变量。我该怎么做呢?我基本上想打印我的所有信息,包括purchaseDate。如果我没有正确提出我的问题,我提前道歉,这是我的第一篇文章。感谢您的帮助。
我有两个类:购买日期和车辆。
我的车辆类有这个方法,旨在打印出我的信息:
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
PurchaseDate.printPurchaseDate();
}
我在 Vehicle 类的“printDetails()”方法中从 PurchaseDate 类打印日期时遇到问题。
/**
* The Purchase data class
*/
public class PurchaseDate {
private int year;
private int month;
private int day;
private static final int CURRENT_YEAR = 2014;
private static final int LAST_MONTH = 12;
private static final int LAST_DAY = 31;
/**
* default constructor
*/
public PurchaseDate() {
}
/**
* @param year to initialize theYear field
* @param month to initialize theMonth field
* @param day to initialize theDay field
*/
public PurchaseDate(int theYear, int theMonth, int theDay) {
setYear(theYear);
setMonth(theMonth);
setDay(theDay);
if (year < 1900 || year > 2014) {
System.out.println("The year value can be no greater than the current year");
} else {
this.year = year; }
if (month < 1 || month > 12) {
System.out.println("The month value must be between 1 and 12");
} else {
this.month = month; }
if (day < 1 || day > 31) {
System.out.println("The day value must be between 1 and 31");
} else {
this.day = day; }
}
//Mutators and Accessors
/**
* @return year
*/
public int getYear() {
return year;
}
/**
* @return month
*/
public int getMonth() {
return month;
}
/**
* @return day
*/
public int getDay() {
return day;
}
/**
* @param the year
*/
public final void setYear(int newYear) {
year = newYear;
}
/**
* @param the month
*/
public final void setMonth(int newMonth) {
month = newMonth;
}
/**
* @param the day
*/
public final void setDay(int newDay) {
day = newDay;
}
/**
* prints the purchase date
*/
public void printPurchaseDate() {
System.out.println("The purchase date is:" + " " + year + "-" + month + "-" + day);
}
}
我基本上希望我的System.out.println 打印出日期
我在我的PurchaseDate 课程中。
【问题讨论】:
标签: java methods int call primitive