- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个程序,用户可以在其中输入日期并将其打印出来。此外,该程序还有一个给定的日期来计算与第二个条目的年份差异。我当前的问题是,当我调用计算方法时,一切似乎都正常,但不幸的是输出始终为 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);
}
这会简化您的 getYeardiff
和 asString
函数:
//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/
我正在完成一项让我难堪好几天的学校作业。任务是使用 View (VAvailableGolfers) 将与从组合框中选择的给定事件/年份无关的高尔夫球手填充到列表框中。以下是表中的数据: 那么,表单上
给定一年和那年的某一天,我如何获得完整的日期?例如:60/2014 = 2014 年 3 月 1 日 和 61/2016 = 2016 年 3 月 1 日 注释:-年和日可以作为单独的参数传递。 -结
我正在使用 ExtJS 3,当我想为日期选择器选择日期时,我只能设置日期和月份(通过左/右箭头)。月/年面板会放下来,但很快就会消失,因此无法选择它。这是最能描述该行为的桌面记录:http://fli
我正在尝试获取用于在 div 中显示当前月份和年份的 jquery 或 java 脚本代码,但目前还不能。我的意思是我想以这种格式显示当前的月份和年份:October 2012 这样每个月我都不需要编
我有这段代码可以从数据库中获取一个字段: $end_date=$row1['end_date']; 如果我打印它,它会给我这样的信息:25-09-2012我需要的是获取月份值、年份和日期。类似于: $
我有两个格式如下的数据集: df1 #> Artist Album Year #> 1 Beatles Sgt. Pepper's 1967 #>
我使用 jQuery 验证插件和 maskedInput 插件对输入进行了自定义日期和时间验证。 有没有办法在自定义验证中进行验证以防止输入大于当前年份的年份? 我的代码: $("#date").ma
我在 xml 解析方面没有经验,所以也许我写的一些东西对某些人来说看起来很愚蠢,也许我的一些术语不太正确。请原谅。 我开发了一个Android应用程序,它需要解析来自YR.no的天气数据。 。该组织提
我需要当前年份,月份和日期为3个不同的变量。下面的代码给出了日期时间 val now = Calendar.getInstance().getTime() 2016年9月29日星期四18:27:38
在模态对话框中使用日期选择器,请不要在 Firefox 19.0.2 中使用更改月/年下拉列表,请参阅: http://jsfiddle.net/469zV/2/ HTML
我希望日期输入在年份有 4 位数字后停止输入。我尝试使用 HTML 中的 min 和 max 组件,但它仍然允许您键入删除第一个数字。参见示例 1。 请参阅示例 2 以了解可以输入超过 4 位数字的年
因此不同是基于唯一的月/年,而不仅仅是一个不同的月份(所以我希望 2011 年 1 月和 2012 年 1 月是不同的) // Test set of data List Compl
在 Programming in the Key of C# 中,作者给出了一个示例(附源代码),说明如何将日期(年、月、日 -- 数字)打包为 32 位整数。在示例中,作者将信息打包如下: int
这里我有一个任务,如果我写这样的代码,年份应该像 1947 年到 2016 年一样绑定(bind)在 DropdownList 中 1947 2016 拍了一整天 最佳答案 例如,
显然,我的 SQL 不是最好的,但我想做的是通过查找条目中的最大年份和月份来获取数据库中的最新日期。现在我有: select max(Month), max(Year) from posts wher
我试图弄清楚是否有任何 ID 发生在任何早年(即 dfo 中的 Duplicate 列)。如果是这样,我想将该行标记为重复行并包括 ID 首次出现的年份(即 Year_Duplicate)。 我确实有
我已经从 df 中提取了列列表,这些列是字符串类型,如下所示:1999-1(如 1999 年第一个月)。 我想删除 2000 年之前的所有列,因此我提取了列列表并使用列表理解来检查字符串的第一个字符是
日期时间是什么时候?我的 DateTimePicker 控件的源更改为 null,我想选择另一个日期,日历下拉列表默认为最后选择的月份和年份。 我正在为 DOB 使用 DateTimePicker,因
我正在获取系统当前日期并尝试在 TextView 中显示它。 尝试下面的代码后 private OnClickListener listener1 = new OnClickListener() {
我想使用 knockoutJS 库验证年份。我面临的问题是,当用户输入错误的年份时,不会对年份进行验证。以下是年份错误的情况。 (01/01/12 becomes 01/01/0012) (01/0
我是一名优秀的程序员,十分优秀!