我正在编写一种方法,可以将日期提前给定的周数。这是我的代码:
public class Date {
int year;
int month;
int day;
public Date (int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
public void addWeeks (int weeks){
int week = weeks * 7;
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, this.day);
calendar.set(Calendar.MONTH, this.month);
calendar.set(Calendar.YEAR, this.year);
calendar.add(Calendar.DAY_OF_MONTH, week);
System.out.println();
System.out.println("Date after adding " + weeks + " weeks is: " + dateFormat.format(calendar.getTime()));
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我将今天的日期传递给年、月和日。(03/08/2019) 然后调用 addWeeks(1) 例如,那么日期应该提前为 (03/15/2019) 但它给了我 (04/15/2019)。由于某种原因,月份总是比我输入的月份多 1。如果我输入月份 …
我是一名新的java程序员.我正在写一个关于餐厅菜单的程序,但我的价格没有正确计算.每次给它0.0,应该是11.0
public class Main {
public static double priceBreadrollType;
public static double priceMeat;
public static double totalPrice;
public static void main(String[] args) {
setTotalPrice();
}
public static void priceBread (){
priceBreadrollType = 1;
}
public static void priceMeat(){
priceMeat = 10;
}
public static void setTotalPrice(){
totalPrice = priceBreadrollType + priceMeat;
System.out.println("The total prize " + totalPrice);
}
}
Run Code Online (Sandbox Code Playgroud)