gpt4 book ai didi

java - 如何计算我的类(class)中的年份差异?

转载 作者:行者123 更新时间:2023-12-01 19:32:13 25 4
gpt4 key购买 nike

我正在编写一个程序,用户可以在其中输入日期并将其打印出来。此外,该程序还有一个给定的日期来计算与第二个条目的年份差异。我当前的问题是,当我调用计算方法时,一切似乎都正常,但不幸的是输出始终为 0。

这是我的代码:

import java.util.Scanner;
public class Dates{
static Scanner scan = new Scanner(System.in);
// Attributes
static int Day;
static int Month;
static int Year;

//Constructor
Dates(int x, int y, int z){
Day = 12;
Month = 12;
Year = 2019;
if(Day > 31) {
do {
System.out.println("Wrong day! repeat entry: ");
Day = scan.nextInt();
}while (Day > 31);
}
if(Month > 12) {
do {
System.out.println("Wrong month! repeat entry: ");
Month = scan.nextInt();
}while(Month > 12);
}
if(Year < 1800) {
do {
System.out.println("Incorrect Year! repeat entry: ");
Year = scan.nextInt();
}while(Year < 1800);
}
}
//Calculates Year difference
public static int getYeardiff(int day, int month, int year) {
System.out.println("Press <0>, if you want to know passed years between two dates. ");
int difference = 0;
int key = scan.nextInt();
if(key == 0) {
System.out.println("Enter a second date: ");
Dates diffr = new Dates(Day, Month, Year);
diffr.Day = scan.nextInt();
diffr.Month = scan.nextInt();
diffr.Year = scan. nextInt();
if(diffr.Year <= Dates.Year) {
difference = Dates.Year - diffr.Year;
System.out.println(difference);
}
}
return difference;
}
//Formats the date into a String.
public static String asString(int day, int month, int year) {
String stringDay = "" + Day;
String stringMonth = "" + Month;
String stringYear = "" + Year;
String stringDate = stringDay + "." + stringMonth+ "." + stringYear;
System.out.println(stringDate);
return stringDate;
}


public static void main(String[] args) {
System.out.println("Enter todays date!");
Dates xmas = new Dates(Day, Month ,Year);
xmas.Day = scan.nextInt();
xmas.Month = scan.nextInt();
xmas.Year = scan.nextInt();
asString(xmas.Day, xmas.Month, xmas.Year);
getYeardiff(Day, Month, Year);
}
}

这是获得更好概览的方法。

public static int getYeardiff(int day, int month, int year) {
System.out.println("Press <0>, if you want to know passed years between two dates. ");
int difference = 0;
int key = scan.nextInt();
if(key == 0) {
System.out.println("Enter a second date: ");
Dates diffr = new Dates(Day, Month, Year);
diffr.Day = scan.nextInt();
diffr.Month = scan.nextInt();
diffr.Year = scan. nextInt();
if(diffr.Year <= Dates.Year) {
difference = Dates.Year - diffr.Year;
System.out.println(difference);
}
}
return difference;
}

如上所述,我得到的输出始终为 0。这是完整的输出:

Enter todays date!
12
12
2019
12.12.2019
Press <0>, if you want to know passed years between two dates.
0
Enter a second date:
12
12
1990
0

我的问题是,我必须更改什么才能从我的计算方法中获得正确的输出?

最佳答案

使用大写字母的字段违反了 java 标准。此外,您的 Dates 类的构造函数不会对传入的参数执行任何操作。相反,它们是从扫描仪分配的。这也是违反惯例的。扫描仪应该在外部请求输入,然后您根据用户输入的内容构建类。

我修改了您的代码如下:

首先,您的 Dates 类可以简化为:

int day;
int month;
int year;

//Constructor
Dates(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}

然后我创建了一个方法来要求输入日期

public static Dates getDates() {
int day = 0, month = 0, year = 0;

System.out.print("Enter the day: ");
day = scan.nextInt();

while (day > 31 || day < 1) {
System.out.println("Wrong day! repeat entry: ");
day = scan.nextInt();
}

System.out.print("Enter the month: ");
month = scan.nextInt();

while(month > 12 || month < 1) {
System.out.println("Wrong month! repeat entry: ");
month = scan.nextInt();
}

System.out.print("Enter the year: ");
year = scan.nextInt();
while(year > 9999 || year < 1800 ) {
System.out.println("Incorrect Year! repeat entry: ");
year = scan.nextInt();
}

return new Dates(day, month, year);
}

这会简化您的 getYeardiffasString 函数:

//Calculates Year difference
public static int getYeardiff(Dates other) {
Scanner scanner = new Scanner(System.in);
System.out.print("Press <0>, if you want to know passed years between two dates. ");
int difference = 0;
int key = scan.nextInt();
if(key == 0) {
System.out.println("Enter a second date: ");

Dates diffr = getDates();

difference = Math.abs(diffr.year - other.year);
System.out.println(difference);
}
return difference;
}

//Formats the date into a String.
public static String asString(Dates other) {
String stringDay = "" + other.day;
String stringMonth = "" +other.month;
String stringYear = "" + other.year;
String stringDate = stringDay + "." + stringMonth+ "." + stringYear;
System.out.println(stringDate);
return stringDate;
}

最后是你的main方法:

public static void main(String[] args) {
System.out.println("Enter todays date!");
Dates xmas = getDates();
asString(xmas);
getYeardiff(xmas);
scan.close();
}

关于java - 如何计算我的类(class)中的年份差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59303075/

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