我很难理解更改日期的用户输入的方法,以及要与另一个int进行比较的int。 我试图让用户输入他的出生日期,然后如果可能的话,将它与开关格式的星座日期进行比较。 通过大多数关于如何将输入更改为int的文章,使用pars和SimpleDateFormat,我无法应用它,如我的代码中所示,当我尝试实现" dateOB"应该已经形成一个int,在switch语句中它没有认识到它......
到目前为止我的代码:
import java.util.*;
import java.text.*;
public class signs {
public static void main(String[] args) {
Scanner userInput = new Scanner (System.in);
// Intro message
System.out.println("Hello you, Lets get to know each other");
// User input begin
//name
String userName;
System.out.println("What is your name ?");
userName = userInput.nextLine();
//date of birth
System.out.println(userName + " please enter you DoB (DD/MM/YYY)");
String dateOB = userInput.next();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
try {
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
System.out.println(dateOB);
} catch (ParseException e) {
e.printStackTrace();
return;
}
System.out.println("So your date of birth is " + dateOB);
// choosing zodiac sign
//starting the switch statement
int convertedDate = dateOB;
String zodiacSign;
switch (convertedDate){
}
}
}
如果有人可以向我解释如何以简单的方式实现这一点,我将不胜感激......
所以我得到你们非常好的建议,我最终得到了对事物的正确理解,只是复杂化实施了一些小建议,使代码功能正常,
到目前为止我得到的是:boolean correctFormat = false;
do{
System.out.println(userName + " please enter you DoB (DD/MM/YYY)");
String dateOB = userInput.next();
try{
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
System.out.println(dateOB);
System.out.println("So your date of birth is " + dateOB);
correctFormat = true;
}catch(ParseException e){
correctFormat = false;
}
}while(!correctFormat);
所以我面临的问题是" dateOB现在是因为它在while循环中,在循环中没有被识别出来,这是检查日期格式是否正确,所以当我尝试将日期转换为数字时: int dayNmonth = Integer.parseInt(new SimpleDateFormat(" ddMM")。format(dateOB));
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);
//int month = cal.get(Calendar.MONTH)+ 1;
到目前为止,我对部分代码遇到了一些麻烦:
Calendar cal = Calendar.getInstance();
cal.setTime(dayNmonth);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH)+ 1;
就像在eclips中一样,它不会让我从" dateOB"获得日期和月份。用这个代码。
有人可以尝试帮我理解问题所在!?
答案 0 :(得分:1)
我认为您可以以“MMdd”格式获取用户输入的日期和月份。然后使用Integer.parseInt()来获取一个数字,然后使用if语句来定位星座,因为它取决于日期和月份。
例如白羊座。 3月21日 - 4月19日。所以你可以从用户的日期得到输入数字,如:
int userVal=0;
if (userDate != null) {
DateFormat df = new SimpleDateFormat("MMdd");
userVal = Integer.parseInt(df.format(date));
}
现在你可以通过以下方式返回白羊座:
if(userVal >= 321 && userVal <= 419) //Mar 21 == 0321 and April 19 == 0419
System.out.println("Your zodiac sign: Aries");
继续其他迹象。
<强>实施强>
public static void main(String[] args) throws ParseException {
Date date;
int mmdd;
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat mmDDFormat = new SimpleDateFormat("MMdd");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your date of birth(dd/MM/yyyy):");
String stringDate = scanner.next();
date = dateFormat.parse(stringDate);
System.out.println("This is your date of birth: " + dateFormat.format(date));
mmdd = Integer.parseInt(mmDDFormat.format(date));
System.out.println("This is the mmdd value: " + mmdd);
//Now getting the Zodiac sign
System.out.println("The zodiac sign of your date of birth is: ");
if (mmdd >= 321 && mmdd <= 419) {
System.out.println("ARIES");
} else if (mmdd >= 420 && mmdd <= 520) {
System.out.println("TAURUS");
} else if (mmdd >= 521 && mmdd <= 620) {
System.out.println("GEMINI");
} else if (mmdd >= 621 && mmdd <= 722) {
System.out.println("CANCER");
} else if (mmdd >= 723 && mmdd <= 822) {
System.out.println("LEO");
} else if (mmdd >= 823 && mmdd <= 922) {
System.out.println("VIRGO");
} else if (mmdd >= 923 && mmdd <= 1022) {
System.out.println("LIBRA");
} else if (mmdd >= 1023 && mmdd <= 1121) {
System.out.println("SCORPIO");
} else if (mmdd >= 1122 && mmdd <= 1221) {
System.out.println("SAGITTARIUS");
} else if ((mmdd >= 1222 && mmdd <= 1231) || (mmdd >= 11 && mmdd <= 119)) {
System.out.println("CAPRICORN");
} else if (mmdd >= 120 && mmdd <= 218) {
System.out.println("AQUARIUS");
} else if (mmdd >= 219 && mmdd <= 320) {
System.out.println("PISCES");
}
}
Nb:我使用的是Netbeans 8.0.1
答案 1 :(得分:1)
我建议您使用简单的Bean ZodiacSign:
class ZodiacSign {
private String name;
private int startMonth;
private int startDay;
private int endMonth;
private int ednDay;
public ZodiacSign(String name, int startMonth, int startDay, int endMonth, int ednDay) {
super();
this.name = name;
this.startMonth = startMonth;
this.startDay = startDay;
this.endMonth = endMonth;
this.ednDay = ednDay;
}
// getter & setter
}
并迭代一个集合,直到找到匹配项,如下所示:
List<ZodiacSign> zodiac = Collections.emptyList();
zodiac.add(new ZodiacSign("AQUARIUS", Calendar.JANUARY, 20, Calendar.FEBRUARY, 18));
zodiac.add(new ZodiacSign("PISCES", Calendar.FEBRUARY, 19, Calendar.MARCH, 20));
// ..
zodiac.add(new ZodiacSign("CAPRICORN", Calendar.DECEMBER, 22, Calendar.JANUARY, 19));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
for (ZodiacSign sign : zodiac) {
if (month >= sign.getStartMonth() && month <= sign.getEndMonth()) {
if (dayOfMonth >= sign.getStartDay() && dayOfMonth <= sign.getEdnDay()) {
System.out.println("Zodiac Sign: " + sign.getName());
}
}
}
答案 2 :(得分:1)
你可以这样做
public static void main(String[] args) throws ParseException {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello you, Lets get to know each other");
String userName;
System.out.println("What is your name ?");
userName = userInput.nextLine();
System.out.println(userName + " please enter you DoB (DD/MM/YYY)");
String dateOB = userInput.next();
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
System.out.println(dateOB);
System.out.println("So your date of birth is " + dateOB);
Calendar c = Calendar.getInstance();
c.setTime(date);
int day = c.get(Calendar.DAY_OF_YEAR);
// IF day is from January 20 - to February 18 this means he or she is aquarius
if (day >= 20 && day <= 49) {
System.out.println("Aquarius");
} else if (day >= 50 && day <= 79) {
//If day if from February 19 to March 20 then pisces
System.out.println("Pisces");
}
//write all zodiac signs ...
userInput.close();
}
答案 3 :(得分:1)
嗯,最后我能够克服这个问题,并充分了解新概念,感谢@LordAnomander的耐心和详细说明,@ cdaiga富有洞察力的替代解决方案和@Dato Mumladze合作。
我到目前为止所达到的正确代码没有生肖比较,我将不得不应用切换方法,我相信,因为我还在学习它将面临一些其他问题,但还有更多要学习,
import java.util.*;
import java.text.*;
import java.lang.*;
public class signs {
public static void main(String[] args) throws ParseException {
Scanner userInput = new Scanner (System.in);
// Intro message
System.out.println("Hello you, Lets get to know each other");
// User input begin
//name
String userName;
System.out.println("What is your name ?");
userName = userInput.nextLine();
//date of birth
Date date = new Date();
String dateOB = "";
boolean correctFormat = false;
do{
System.out.println(" please enter you DoB (dd/MM/yyyy)");
dateOB = userInput.next();
try{
date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
System.out.println("day and month " + dateOB);
correctFormat = true;
}catch(ParseException e){
correctFormat = false;
}
}while(!correctFormat);
//Choosing sign
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH)+ 1;
// announcing sign
//converting the day and month to a number to compare
int zodiacNum = day * 100 + month;
System.out.println(" zodiac number is " + zodiacNum);
//closing userInput
userInput.close();
}
}
再次感谢大家,我希望这对有基本知识的人有用,并帮助他们理解一些问题。
答案 4 :(得分:0)
将输入转换为Date
后,您还可以使用Calendar
获取信息。
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH) + 1;
编辑#1:
要获得您想要的号码,您必须进行额外的计算,即
int zodiacNumber = month * 100 + day;
输入05/11/1984将导致1105。反之亦然,如果您愿意,可以使用day * 100 + month
。
或者您可以使用
直接将日期解析为您希望的格式 int zodiac = Integer.valueOf(new SimpleDateFormat("ddMM").format(date))
这是一个更短的解决方案。
编辑#2:当您回复一个带有评论的问题时,您需要循环以确保以正确的格式输入日期,请执行以下操作:
boolean correctFormat = false;
do {
// ask the user for input
// read the input
try {
// try to format the input
correctFormat = true;
} catch (ParseException e) {
correctFormat = false;
}
} while (!correctFormat);
如果抛出异常,您可以确定日期没有以正确的方式输入,因此,通过设置辅助变量,您可以确保在可转换之前请求日期,因此,正确。
编辑#3:如果您使用Date
而不是SimpleDateFormat
,则需要使用String
。
int dayNmonth = Integer.parseInt(new SimpleDateFormat("ddMM").format(dateOB));
无效!你必须使用
int dayNmonth = Integer.parseInt(new SimpleDateFormat("ddMM").format(date));
所以你的程序必须如下所示:
Date date = new Date();
String dateOb = "";
boolean correctFormat = false;
do {
(...)
date = new SimpleDateFormat("dd/MM/yyyy").parse(dateOB);
(...)
} while (...);
int dayNmonth = Integer.parseInt(new SimpleDateFormat("ddMM").format(date);
(...)
cal.setTime(date);
(...)
重要的是要知道date
是真实日期,而dateOB
只是用户输入的内容 - 此String
不能用作真实的约会!