我正在编写一个提示生日(如02 14)的程序,然后用您输入的日期以及该日期的十二生肖打印出来。这是我的代码。
public class Horoscope{
public String getBirthday (String birth)
{
String month = birth.substring(0, 2);
String day = birth.substring(3, 5);
if(month.equals("01"))
{
month = "January ";
}//end of January
else if (month.equals("02"))
{
month = "Feburary ";
}//end of Feb.
else if (month.equals("03"))
{
month = "March ";
}//end of March
else if (month.equals("04"))
{
month = "April ";
}//end of April
else if (month.equals("05"))
{
month = "May ";
}//end of may
else if (month.equals("06"))
{
month = "June ";
}//end of June
else if (month.equals("07"))
{
month = "July ";
}//end of July
else if (month.equals("08"))
{
month = "August ";
}//end of August
else if (month.equals("09"))
{
month = "September ";
}//end of Sept
else if (month.equals("10"))
{
month = "October ";
}//end of Oct
else if (month.equals("11"))
{
month = "November ";
}//end of nov
else if (month.equals("12"))
{
month = "December ";
}//end of Dec
else
{
System.out.println("Invaid month type.");
}//end of invalid
if(day.equals("01"))
{
day = "1st";
}//end of 1
else if (day.equals("02"))
{
day = "2nd";
}//end of 2
else if(day.equals("03"))
{
day = "3rd";
}
else if(day.equals("04"))
{
day = "4th";
}//end of 4
else if(day.equals("05"))
{
day = "5th";
}//end of 5th
else if(day.equals("06"))
{
day = "6th";
}
else if(day.equals("07"))
{
day = "7th";
}
else if(day.equals("08"))
{
day = "8th";
}
else if(day.equals("09"))
{
day = "9th";
}
else if(day.equals("10"))
{
day = "10th";
}//end of 10th
else if(day.equals("11"))
{
day = "11th";
}
else if(day.equals("12"))
{
day = "12th";
}
else if(day.equals("13"))
{
day = "13th";
}
else if(day.equals("14"))
{
day = "14th";
}
else if(day.equals("15"))
{
day = "15th";
}//end of 15
else if(day.equals("16"))
{
day = "16th";
}
else if(day.equals("17"))
{
day = "17th";
}
else if(day.equals("18"))
{
day = "18th";
}
else if(day.equals("19"))
{
day = "19th";
}
else if(day.equals("20"))
{
day = "20th";
}//end of 20th
else if(day.equals("21"))
{
day = "21st";
}
else if(day.equals("22"))
{
day = "22nd";
}
else if(day.equals("23"))
{
day = "23rd";
}
else if(day.equals("24"))
{
day = "24th";
}
else if(day.equals("25"))
{
day = "25th";
}//end of 25th
else if(day.equals("26"))
{
day = "26th";
}
else if(day.equals("27"))
{
day = "27th";
}
else if(day.equals("28"))
{
day = "28th";
}
else if(day.equals("28"))
{
day = "28th";
}
else if(day.equals("29"))
{
day = "29th";
}
else if(day.equals("30"))
{
day = "30th";
}//end of the 30th
else if(day.equals("31"))
{
day = "31st";
}
else
{
System.out.println("Invalid day type.");
}
return month + day;
}
private int parseInt(String substring) {
// TODO Auto-generated method stub
return 0;
}
public String getZodiac(String zodiac)
{
String zodiacMonthS = zodiac.substring(0, 2);
String zodiacDayS = zodiac.substring(3, 5);
int zodiacMonth = parseInt(zodiacMonthS);
int zodiacDay = parseInt(zodiacDayS);
String zodiacFinal;
if ((zodiacMonth == 12 && zodiacDay >= 22 && zodiacDay <= 31) || (zodiacMonth == 1 && zodiacDay >= 1 && zodiacDay <= 19))
zodiacFinal = "Capricorn";
else if ((zodiacMonth == 1 && zodiacDay >= 20 && zodiacDay <= 31) || (zodiacMonth == 2 && zodiacDay >= 1 && zodiacDay <= 17))
zodiacFinal = "Aquarius";
else if ((zodiacMonth == 2 && zodiacDay >= 18 && zodiacDay <= 29) || (zodiacMonth == 3 && zodiacDay >= 1 && zodiacDay <= 19))
zodiacFinal = "Pisces";
else if ((zodiacMonth == 3 && zodiacDay >= 20 && zodiacDay <= 31) || (zodiacMonth == 4 && zodiacDay >= 1 && zodiacDay <= 19))
zodiacFinal = "Aries";
else if ((zodiacMonth == 4 && zodiacDay >= 20 && zodiacDay <= 30) || (zodiacMonth == 5 && zodiacDay >= 1 && zodiacDay <= 20))
zodiacFinal = "Taurus";
else if ((zodiacMonth == 5 && zodiacDay >= 21 && zodiacDay <= 31) || (zodiacMonth == 6 && zodiacDay >= 1 && zodiacDay <= 20))
zodiacFinal = "Gemini";
else if ((zodiacMonth == 6 && zodiacDay >= 21 && zodiacDay <= 30) || (zodiacMonth == 7 && zodiacDay >= 1 && zodiacDay <= 22))
zodiacFinal = "Cancer";
else if ((zodiacMonth == 7 && zodiacDay >= 23 && zodiacDay <= 31) || (zodiacMonth == 8 && zodiacDay >= 1 && zodiacDay <= 22))
zodiacFinal = "Leo";
else if ((zodiacMonth == 8 && zodiacDay >= 23 && zodiacDay <= 31) || (zodiacMonth == 9 && zodiacDay >= 1 && zodiacDay <= 22))
zodiacFinal = "Virgo";
else if ((zodiacMonth == 9 && zodiacDay >= 23 && zodiacDay <= 30) || (zodiacMonth == 10 && zodiacDay >= 1 && zodiacDay <= 22))
zodiacFinal = "Libra";
else if ((zodiacMonth == 10 && zodiacDay >= 23 && zodiacDay <= 31) || (zodiacMonth == 11 && zodiacDay >= 1 && zodiacDay <= 21))
zodiacFinal = "Scorpio";
else if ((zodiacMonth == 11 && zodiacDay >= 22 && zodiacDay <= 30) || (zodiacMonth == 12 && zodiacDay >= 1 && zodiacDay <= 21))
zodiacFinal = "Sagittarius";
else
zodiacFinal = "Illegal date";
return zodiacFinal;
}
}
这是我的测试员:
import java.util.Scanner;
public class HoroscopeTester {
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println("What is your birthday? Type in month and year as two numbers. Ex. 01 01(January 1st)");
Scanner scanner = new Scanner(System.in);
String birth = scanner.nextLine();
String zodiac = scanner.nextLine();
Horoscope horoscope = new Horoscope();
System.out.println(horoscope.getBirthday(birth));
System.out.print(horoscope.getZodiac(zodiac));
scanner.close();
}
}
日期打印正常,但Zodiac部分出错。我也注意到在输入日期后我需要按两次输入。我不知道为什么会这样。这是控制台为我显示的内容:
What is your birthday? Type in month and year as two numbers. Ex. 01 01(January 1st)
02 14
February 14th
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.substring(Unknown Source)
at Horoscope.getZodiac(Horoscope.java:246)
at HoroscopeTester.main(HoroscopeTester.java:21)
答案 0 :(得分:0)
问题是字符串的值超出了它的索引,即如果你已经初始化了一个大小为10的字符串,并且你试图存储超过10的长度。抛出此错误检查main函数中的字符串初始化并将其初始化为更大的大小。