gpt4 book ai didi

java - 将扫描仪传递给方法

转载 作者:行者123 更新时间:2023-12-01 06:29:30 25 4
gpt4 key购买 nike

谁能告诉我为什么日期打印0/0/0
缺少哪部分代码,因此使用 Scanner 输入的值被传递到方法 DisplayDate 并打印在屏幕上?

import java.util.Scanner;

public class DateClass {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Enter day:");
int day = scanner.nextInt();
System.out.println("Enter month:");
int month = scanner.nextInt();
System.out.println("Enter year:");
int year = scanner.nextInt();

Date d = new Date(day,month,year);
d.DisplayDate();

}

}

class Date {

private int day;
private int month;
private int year;


public Date(int day, int month, int year) {
}

public int getDay() {
return day;
}

public void setDay(int day) {
this.day = day;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}


public void DisplayDate() {
System.out.println(getDay() + "/" + getMonth() + "/" + getYear());
}

}

最佳答案

你没有保存你的日、月、年。您应该在构造函数中执行此操作:

public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}

关于java - 将扫描仪传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24286082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com