[英]How do you compare values (greater than/less than) of strings that have been turned into integers?
So, I have to make a code that takes in 2 dates (month/day/year) from the user and returns "true" if the FIRST date is less than the second date. 因此,我必须编写一个从用户处获取2个日期(月/日/年)的代码,如果FIRST日期小于第二个日期,则返回“ true”。 In any other case the date will be "false" or "they are the same". 在任何其他情况下,日期将为“假”或“它们相同”。 I was instructed that I could NOT demand the user to do a specified format (ie mm/dd/yyyy) and that instead I should focus on the "/"s. 有人指示我不能要求用户使用指定的格式(例如,mm / dd / yyyy),而应将重点放在“ /”上。
The problem is, it always returns the "they are the same" answer no matter what I put in. Any tips and advice are greatly appreciated! 问题是,无论我输入什么内容,它总是返回“他们是相同的”答案。非常感谢任何提示和建议! I'm just a beginner programmer, so I'm not sure what's wrong. 我只是一个初学者,所以不知道出什么问题了。
Here's The code: 这是代码:
import java.util.Scanner;
public class Mari_WS15IsDateEarlier {
public static void main (String[] args) {
//initial variables, asks for variable input and determines month/day/year
Scanner dateInput = new Scanner(System.in);
String answr;
//splits string input into month, day and year
System.out.println("Enter a month, day, and year(separate with a slash) :");
String date1 = dateInput.next();
String[] splitStrings = date1.split("/");
String month1 = splitStrings[0];
String day1 = splitStrings[1];
String year1 = splitStrings[2];
System.out.println("Enter another month, day, and year (separate with a slash) :");
String date2 = dateInput.next();
String[] splitStrings2 = date1.split("/");
String month2 = splitStrings[0];
String day2 = splitStrings[1];
String year2 = splitStrings[2];
//turns string into integer for testing (greater than/less than)
int mn1 = Integer.parseInt(month1);
int mn2 = Integer.parseInt(month2);
int dy1 = Integer.parseInt(day1);
int dy2 = Integer.parseInt(day2);
int yr1 = Integer.parseInt(year1);
int yr2 = Integer.parseInt(year2);
//Determine if the set of variables is a geometric sequence
if (yr1 < yr2) {
answr = "true";
}
else if ((yr1 == yr2)&&(mn1 < mn2)) {
answr = "true";
}
else if ((mn1 == mn2)&&(dy1 < dy2)) {
answr = "true";
}
else if (dy1 == dy2) {
answr = "ERROR: Dates are identical.";
}
else {
answr = "false";
}
//Prints out the answer
System.out.println(answr);
Great Question and fantastic code! 伟大的问题和梦幻般的代码! The solution is simple enough, from the looks of your code you simply copied date1
and pasted for date2
. 该解决方案非常简单,从代码的外观上,您只需复制date1
并粘贴到date2
。 However you didn't change all of the variables so the code was comparing date1
to date1
hence your error. 但是,您并未更改所有变量,因此代码将date1
与date1
进行了date1
,因此出现了错误。 Make sure to change both date1
to date2
and splitStrings
to splitStrings2
. 确保将date1
更改为date2
并将splitStrings
为splitStrings2
。
Also just a little suggestion specific to your code, I would look again at the identical dates if statement as you are only comparing days. 还是针对您的代码的一些建议,如果您只比较天数,我将再次查看相同的if语句日期。 Try date1=1/2/2
and date2=2/2/2
and you will see the issue! 尝试date1=1/2/2
date2=2/2/2
和date2=2/2/2
,您将看到问题!
Watch-out for the 2nd date, you need to change date1.split("/");
当心第二个日期,您需要更改date1.split("/");
with date2.split("/")
与date2.split("/")
Replace your 2nd date top two lines with following: 将您的第二个日期的前两行替换为以下内容:
String date2 = dateInput.next();
String[] splitStrings2 = date2.split("/");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.