简体   繁体   English

您如何比较已转换为整数的字符串的值(大于/小于)?

[英]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. 但是,您并未更改所有变量,因此代码将date1date1进行了date1 ,因此出现了错误。 Make sure to change both date1 to date2 and splitStrings to splitStrings2 . 确保将date1更改为date2并将splitStringssplitStrings2

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/2date2=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.

相关问题 你如何进行大于或小于字符串的比较 - How do you do a compare of more than or less than with string 如何在android中比较字符串大于 - How can you compare strings in android with greater than 小于1且大于0的值的计算百分比 - Percentage Calculation for values less than 1 and greater than 0 你能比较一个变量,看看它是大于还是小于一个值,而不用两次列出变量吗? - Can you compare a variable to see if it's greater than and less than a value, without listing the variable twice? 如何防止扫描仪接受字符串,负整数或小于2的数字? - how do i prevent the scanner from accepting strings, negative integers, or numbers less than 2? 如何计算小于n且总和大于n * 2的3个整数的组合数? - How to calculate the number of combinations of 3 integers less than n whose sum is greater than n * 2? 在内部大于/小于多少 - how greater than/less than works internally 将两个字节 arrays 与 java 中的大于或小于运算符进行比较 - compare two byte arrays with greater than or less than operator in java 比较两个大于或小于 Android Studio 的字符串 - Compare Two Strings with Greater than or Lower than Android Studio 如何读取浮点值并打印小于或大于该值的最接近的数字? - How do I read a floating point value and print the closest numbers less than or greater than that value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM