gpt4 book ai didi

java - 我不断收到 'Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException : 30?

转载 作者:行者123 更新时间:2023-12-01 10:59:26 25 4
gpt4 key购买 nike

这是一个附加类,当我使用 Eclipse 编译器时它工作得很好。但由于某种原因,当我将其输入 edhesive 的编译器时,错误不断弹出。

import java.util.Scanner;

class Main {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("What month were you born in? (number)");
int month = scan.nextInt();

System.out.println("What day? (number)");
int day = scan.nextInt();

String[] arrayM = {
"January", "February", "March", "April", "June", "July", "August", "September", "October", "November", "December"
};
String[] arrayD = {
"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eight", "ninth", "tenth", "eleventh", "twelvth", "thirteenth",
"fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth", "twenty-first",
"twenty-second", "twenty-third", "twenty-fourth", "twenty-fifth", "twenty-sixth", "twenty-seventh",
"twenty-ninth", "thirtieth", "thirty-first"
};

System.out.println("Your birthday is: " + arrayM[month - 1] + " " + arrayD[day - 1]);

if (month == 1) {
if (day >= 1 && day <= 19)
System.out.println("Capricorn");
else if (day >= 20 && day <= 31)
System.out.println("Aquarius");
} else if (month == 2) {
if (day >= 1 && day <= 18)
System.out.println("Aquarius");
else if (day >= 19 && day <= 31)
System.out.println("Pisces");
} else if (month == 3) {
if (day >= 1 && day <= 20)
System.out.println("Pisces");
else if (day >= 21 && day <= 31)
System.out.println("Aries");
} else if (month == 4) {
if (day >= 1 && day <= 21)
System.out.println("Aries");
else if (day >= 22 && day <= 31)
System.out.println("Taurus");
} else if (month == 5) {
if (day >= 1 && day <= 20)
System.out.println("Taurus");
else if (day >= 21 && day <= 31) ;
System.out.println("Gemini");
} else if (month == 6) {
if (day >= 1 && day <= 20)
System.out.println("Gemini");
else if (day >= 21 && day <= 31)
System.out.println("Cancer");
} else if (month == 7) {
if (day >= 1 && day <= 22)
System.out.println("Gemini");
else if (day >= 23 && day <= 31)
System.out.println("Leo");
} else if (month == 8) {
if (day >= 1 && day <= 22)
System.out.println("Leo");
else if (day >= 23 && day <= 31)
System.out.println("Virgo");
} else if (month == 9) {
if (day >= 1 && day <= 22)
System.out.println("Virgo");
else if (day >= 1 && day <= 31)
System.out.println("Libra");
} else if (month == 10) {
if (day >= 1 && day <= 22)
System.out.println("Libra");
else if (day >= 23 && day <= 31)
System.out.println("Scorpio");
} else if (month == 11) {
if (day >= 1 && day <= 21)
System.out.println("Scorpio");
else if (day <= 22 && day <= 31)
System.out.println("Sagittarius");
} else if (month == 12) {
if (day >= 1 && day <= 21)
System.out.println("Sagittarius");
else if (day >= 22 && day <= 31)
System.out.println("Capricorn");
} else {
System.out.println("error");
}


}
}

最佳答案

看起来 arrayD 中缺少“twenty-eighth”,因此只有 30 个元素。

关于java - 我不断收到 'Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException : 30?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com