gpt4 book ai didi

java - 为什么当我在 Eclipse 上运行代码时会保留异常消息?

转载 作者:行者123 更新时间:2023-12-01 11:08:24 26 4
gpt4 key购买 nike

当我在 Eclipse 上运行此代码时,它会给我以下异常消息:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1000
at HW2.howManyTimesDoNumbersIncrease(HW2.java:221)
at HW2.main(HW2.java:190)

请运行您的程序,告诉我我的程序出了什么问题。请帮忙!

代码

public class HW2 {

// Class variables
// DO NOT CHANGE THE FOLLOWING ARRAY!!
public static int[] first = { 3391, 3391, 3391, 0, -1036, -3676, -4840,
3798, 0, 0, -1350, -1350, 4320, -265, -265, 3222, 3222, -77, 0, 0,
-3344, -3018, 1479, -2085, -2085, -2085, -2085, -2085, 0, 3205,
-3775, 1165, 1165, 0, 0, 747, -1584, -3320, 3301, 0, -194, 1595,
1595, 0, 440, 722, 722, 722, 722, 3130, -2978, 4215, 4215, 4215,
766, 766, -157, -3206, 0, 0, -2630, 3348, 3348, 0, 0, -3942, -1545,
326, 326, 4901, -4048, 0, -1388, -1388, 0, 0, 0, 0, 4604, -2998, 0,
-963, 0, 2555, -3578, 875, 410, 410, 2610, -4244, -4244, -4244,
3474, 4073, -3865, -1618, -467, 4630, -2874, -2874, 1660, 1660,
4236, 4236, 0, -1532, 2544, -1869, -1869, 0, -4064, 3383, -1648,
-4642, -4090, -4090, -4090, 1611, -587, 0, 0, 0, 551, 551, 551,
551, 0, 1418, 2953, 3892, -547, -547, 1847, 1719, -3701, 2335,
3193, -4723, -3818, -3818, 1140, 1140, 4154, 3947, 0, 0, -2992,
-2992, 4351, 4351, 213, 213, 213, -2177, 3029, 1275, -1086, -4651,
-4651, 3380, 669, -3962, -3962, -3962, 0, 0, 0, 1764, 496, -4707,
1554, 4758, 0, 280, 3979, -4812, 1678, 43, -979, -2610, 725, 725,
4653, -2571, -2571, -4435, -4799, 4383, -2089, -2089, -2089, -2089,
-2089, 4735, -3959, 3860, -3702, 2334, -3329, -3329, -3329, 3786,
3786, -2057, 4172, 4172, -787, 31, 31, 1702, 3410, 3410, 2216,
-1739, 770, 770, -861, 2107, 2107, -2089, 2432, 2432, 2543, 3422,
2728, 2728, -1292, 3601, 0, 0, 1218, -1979, -2632, -2632, -214,
-214, 0, -770, 4007, -4358, -1776, -867, -3334, -4020, 997, 3607,
-794, 3828, 1740, 0, -38, -38, 870, 870, 870, 1691, 0, -3390, -290,
-290, 4400, -2152, -1433, -1433, -265, -265, -4259, -2589, 4895,
735, 735, 3246, -3913, -4163, -4163, 2084, -4086, -4086, 4486,
2632, 2614, 3069, -3298, -4401, -4881, 2646, 2646, -1323, 4204, 0,
0, -4828, 2662, 470, -3909, -3909, -787, -3675, -3675, -3861,
-3861, 0, 0, -3093, -1750, -59, -3406, -1558, 2399, 4791, 2491,
1287, 1287, 0, -3019, -1008, 0, 797, 1185, -439, -439, 0, 2470,
3553, -4848, 2519, -3162, -2911, -2556, 2229, 1412, 0, 0, -350, 0,
0, -3403, -3403, 3270, 3270, 2898, 2898, 2898, -1620, 2507, 1180,
1180, 384, -4952, 2221, 3447, 3447, 3447, 3447, 1725, -2329, -2329,
-902, 4760, 1092, 1092, 0, 420, 4324, 2048, 99, -3813, -1376,
-1376, -1376, -1376, 564, 3107, -852, 3953, 0, -364, 3388, -2440,
-2440, 1077, 0, 4340, -2646, 4644, -266, -409, 492, -3000, 1073,
-1851, 1950, 1950, -1583, 4937, 3840, 3840, 0, 0, 0, 0, -2505,
-1990, 1159, 1797, 0, 0, -146, 4521, -1095, -2776, 0, 2606, -3931,
-3931, -3931, 2557, 2557, 2557, 2579, 1438, -980, 498, 0, -3139,
4407, 4407, -9, 2287, 1633, -3942, -3942, -872, 2193, 0, 3894,
-1069, -1069, -3094, 3376, 3376, 0, 0, -3017, -3017, -4278, -445,
-4408, 2724, 0, 2678, 2398, -1388, 4676, 2958, 2958, 971, 3491,
3491, 3793, 531, 0, -1278, -3552, -4599, -3125, 2763, -2599, 4505,
-2964, -2964, 0, -1208, 0, 0, 3727, 3727, 0, 0, -562, 0, 1377,
-239, -2957, -4129, -4129, -4129, -3265, 3275, -4140, -4140, -4140,
2421, 2421, 2421, -1462, 1507, 1507, 1507, -4975, 2055, 341, 341,
-2183, -2183, -2183, 348, -2964, -2964, 967, 3650, 3650, 0, 545, 0,
-3443, 3970, -1816, 4905, 4905, -621, -621, -2103, 33, 1260, 1260,
-3546, -3546, -922, -3519, 0, 0, 2382, 3425, -3021, -922, 4183,
989, -1425, -1425, 0, 0, 0, 1784, 1784, 1784, 841, -616, -616,
4488, 3820, 3820, 3820, 3768, 1050, -680, 3757, 2482, -2635, 1408,
1408, -3100, -4242, -4242, -50, -50, -2013, -2013, -2013, -484,
-4434, -4434, -4434, 1562, 1562, 1100, -763, -2597, 3203, 3203,
3203, 2512, 2512, -1100, -1100, -1100, -1100, -1100, -1100, -3462,
-3859, -1716, 4833, -1843, -1843, 421, 867, 4661, 566, 566, 2885,
2885, 2176, -639, 4285, 4285, 4285, 3229, 3229, 0, 0, -3298, -3298,
0, -1452, -3456, -1922, -1370, 4485, 4485, 422, 422, 322, 2044,
3156, 1588, -2406, -2406, -2406, 2136, 4283, 2991, -969, 0, 0,
-369, -369, 0, 4202, 85, 85, 41, -1917, -1917, -773, -3183, 1245,
-1991, 2160, -2860, -2480, 2657, 2657, 2657, 2657, 4975, 760, 2750,
-3876, 3738, 1255, -3385, -1946, 0, -1511, -4376, -3181, -3830,
1862, 1862, -3265, -3265, -3265, 3379, 956, 4833, 4833, -969, 911,
421, 421, -4156, 4045, 2478, -4151, -2899, -2721, -2721, -2721,
-2399, -4976, 2447, 2447, 3534, 0, 0, -4856, -3308, 4592, 4592,
2969, 2969, -4851, 201, 47, -1960, -1960, -1960, -1960, 0, -1181,
-4296, 0, 0, 0, -87, 0, -4796, -1630, -3903, 720, 720, 0, 0, 4341,
4341, -911, -911, -2980, -2222, -2222, -2222, 0, 0, 0, -1198, 1609,
2435, 0, 1623, 1623, 0, 1256, -3594, 3234, -316, -2103, 3289, 0,
3765, 2156, 2156, 2158, 2158, 2158, -1958, -526, -2735, -2735, 98,
3157, 4056, 3613, -3940, -3940, 1548, 2735, -4805, -475, -4285,
-4115, 0, -806, 813, -2120, -2120, 2910, -1645, -1645, 926, 926,
-1873, -1873, 556, 556, -1857, 2339, 0, 4214, 3887, 2540, -4717,
3958, 3958, 3958, -963, -963, 0, 4882, -4490, -3035, -4224, -263,
3646, 894, 0, 0, 0, 2349, 2493, -1124, 1358, -4463, 3136, 1304,
-4621, -4621, -4621, 3845, 3845, -368, -4793, 1983, -1186, -1200,
4909, 4909, 4909, 3178, -3670, -42, -1968, 3403, 812, -38, -1570,
-2693, -674, -4884, 2048, 2276, 1820, 1820, 1820, 79, -1316, -834,
-477, 0, -1873, 3307, 4222, -234, 961, -2372, 4199, 4199, -1714,
4668, 4668, -1556, -1556, -4089, 4389, 4026, 0, -3158, -1678,
-1678, -2960, -2960, -2960, -3427, -3427, 4947, 0, 0, -138, -4103,
-3391, -3391, 0, 0, -3016, -3990, 4396, -1099, 3406, -3632, -2860,
69, 4315, 4690, -891, -48, 0, 1470, -3638, 0, 4735, 4735, 631, 631,
-4769, 2231, 2231, 2231, 0, 0, 0, 0, 1903, 0, 0, 0, 2195, 4142,
-1396, -1756, 2954, -4427, 178, 4857, 4813, 4813, 547, 547, 547,
2923, -4627, 1836, -3243, -3243, 3002, 3002, 2600, 2600, 2600, 971,
0, 0, -1, -1, 4351, 4351, 4351, 2351, -2209, -1771, 3061, -3381,
-3381, -4978, 923, -2470, 512, 512, 2468, 3987, 3987, 3987, 1993,
-3338, -3338, 0, -1696, 3087, 1165, -4491, -4146, -4146, -2412,
-4969, 938, 1351, -40, 0, 4035, -3848, 4345, 0, 1176, 2887, 4333,
4333, 4281, 4281, -354, -354, -371, 4692, 415, -990, -975, 3098,
3098, 0, 0, 0, 4626, };

public static int[] second = { 0, 3113, 3352, 3352, 340, -2872, -2872,
4976, 4976, 2541, 4330, -2917, 2139, 2139, 3853, 1393, 4730, 4730,
-1734, -1734, 4932, 4663, 0, 0, -4982, 1489, 1489, 1489, -65, -65,
-883, 203, 203, 0, 2072, 2072, 2072, -3012, -4878, -225, -225, 0,
0, -3344, -4995, 2638, 2638, -1350, -1350, -2767, 0, 0, -4759,
-4759, 1377, 2545, 2439, 2439, 794, 794, 1342, -2773, -2026, 4404,
-4974, -4974, 3022, 3022, -159, 2154, -3037, -3383, -3383, 0,
-2319, 1905, 0, 2325, -708, -1766, 1205, 1205, -152, 0, 3335, 0, 0,
-303, 1567, 1567, -2042, 0, -4032, 658, -299, 3411, 693, 1213, 0,
-3541, 2366, 2366, 2366, 249, -2454, -2984, -3969, -3869, -3869,
-2230, -1132, 1913, 0, -3508, -3508, 1406, -3917, -3917, -703,
-4040, 3070, 3070, 3504, 331, -3142, 3014, 0, 0, -3759, -3977,
4875, -2916, -2916, 1003, 1547, -501, 0, -881, -2926, -2810, 712,
712, 712, 4570, 1726, 0, -2736, 4358, -3164, 2856, 2592, 0, 0, 0,
3972, 3972, -3143, -3143, -1463, -3342, -3342, -3342, -4867, -4867,
4776, -2911, -2911, -2911, -55, 2425, 2425, 2425, 2685, 2685,
-4424, 0, 0, 4933, -402, 0, -4278, -3043, 2496, 2496, 2496, 3768,
1678, 1795, 3979, 0, 4131, 4131, 0, -1521, 2763, 2763, -3518,
-3518, -3995, 0, 0, -4799, 559, 275, -4180, 0, -3733, -3733, -3704,
-3704, 1890, 168, 2795, 2795, -1831, -1831, 1596, -893, -71, -512,
-1825, -1825, -1825, -2983, -4707, -4652, 806, 4826, 4959, -2488,
652, 652, -1725, -3984, 0, 696, 4408, -3788, 3764, 0, 1316, -2776,
0, 1849, -2470, -4063, 0, 2248, -2183, -2183, 0, -3837, -3837,
-339, 1792, -2659, 358, 0, -4600, 2123, 2123, 3256, -1720, -1720,
-1720, 4045, 4045, 3011, -3275, 0, 0, 4615, -4657, 4560, 4560,
-3570, 0, 0, 0, -3438, 1677, 3475, 3475, 3475, -3965, 3509, 3070,
3167, 442, -630, 2551, 0, 3260, -2432, -2432, -1347, 4358, -85,
-85, 485, 3740, 1675, 1124, 1124, 2627, 4994, -2404, 1638, 4453,
-1065, -609, 4294, -1169, 4593, -225, -225, -225, 1077, -60, -4331,
1608, 1876, 3035, -3045, -3045, 4418, -3622, -3622, 0, 0, 0, 4207,
4207, -430, 0, -669, 0, 0, 0, 630, 630, 4581, -1534, 635, -109,
-109, -109, -2013, -2208, 3685, 3685, 585, 585, 585, 4999, 4395,
4395, -3385, -2789, 2014, 0, -1382, -2869, -2869, 0, 0, -1884,
-1782, 3576, 3576, -3926, 0, 4259, -4770, -4770, -4770, 609, 0,
4005, 4005, 2970, -883, -4679, -12, -12, -890, -2431, 2076, 3011,
-1787, 237, -2781, -2917, -558, 1163, -4513, 3420, 3420, 4370,
4370, 1024, 1024, 672, 672, 0, 4570, 4570, 4570, -2277, 882, 3456,
0, 0, 0, 3594, -3209, -3133, -3852, 3045, 3045, -4142, -4142, 2054,
-4471, 1997, 0, 1852, 3386, -1709, -2972, 0, -4104, 178, -1491,
3984, 3984, 3984, 3984, 3984, 3984, 3984, 0, 0, 3692, 0, 2964,
1548, -1106, 4484, 0, 0, 2600, -2594, 3632, 3632, 2253, 532, 210,
-81, 0, 0, 0, -74, 280, -2884, 1715, -1045, -431, -431, 3357, 3695,
3695, 3695, 0, 385, 385, -307, -4858, -2792, 874, -465, -833, 74,
74, 74, -742, -742, -742, 441, 0, -4041, -4041, -4106, 360, 0, 620,
620, 620, 620, 2498, 4531, -1598, -4643, 3714, 3714, 1309, 3206,
3206, 3542, -2609, -2609, -1165, 0, 0, 0, 0, 0, -2333, 3554, 3554,
3554, 3554, 1828, -3542, -3542, -629, 1589, 2508, -1528, -799,
-799, 0, -733, -4520, 2242, 2242, 2226, 1972, 4800, 4800, 4471,
-1618, 4229, 4229, 4459, 0, 3, 3, 3171, 4739, -279, 3049, -1220, 0,
113, -2272, 2675, -4903, -2498, -3219, -3903, -2275, -2040, 0,
-1973, 0, -4041, 3277, 3277, 3277, -3240, -669, -669, -669, -4814,
-3918, 2542, 4695, 4695, 2873, 2926, 2789, 2789, -2080, -4957,
-4957, -4603, 4290, 2043, 3375, -827, -827, 4175, 185, 185, 1932,
-2172, -2172, -3174, -4826, -4813, 3740, -4327, -2278, 0, -1875,
-1875, -3950, -4829, -261, 3432, -1274, -1274, -2502, -1351, -4286,
-4898, 1371, -988, 416, -1065, 76, 4318, 0, 0, 0, 0, -2579, -2579,
0, 0, 0, 1918, 2781, 2781, -2986, -4446, 1261, 1261, 1261, 1261,
1261, 4286, -2951, 4707, 4707, 4466, 4466, -772, 4691, -4913,
-1642, -3092, -3092, 0, 565, 565, -2026, -2026, 0, 742, 742, -3122,
-4087, -4087, -4087, -4119, -4097, -175, -175, -175, 1776, 1776,
1776, 561, 0, 0, -838, 3071, 3071, 3654, 3654, 708, 708, 4532, 0,
3213, 4535, 4535, 1969, -2980, 2485, -4969, -4969, -1097, -39,
3932, 3116, -1807, 0, 0, -2869, 1274, 1274, 1700, 1700, 1700, 3985,
-2590, 2896, 0, 3012, -2029, -102, 631, 631, -1159, -1159, 394,
2800, -4835, 1446, -1185, -1185, -4091, -4380, -1061, 463, -20,
2527, -4468, 1062, 4188, 33, -4370, -4370, 0, -4230, 4405, 0, 0, 0,
0, -1644, 178, 0, -369, -369, -4085, -3883, -3883, 2150, 633,
-4599, -4599, 0, -3318, -4545, 4759, 62, -865, 1716, 1716, -3735,
-1204, -1204, -435, -779, 0, 3438, 0, 0, -567, 0, -927, -3647, 0,
-3959, -3959, 0, 1074, 2355, 2887, 4760, 0, 0, -1100, -1100, -4905,
-4905, -2193, -2193, -2193, -2193, 1046, -2771, -2832, -831, -3156,
-3156, 4648, -4179, 1680, 435, 649, 1309, 1309, -1893, 1823, 1823,
1823, 1823, 0, 0, -414, -4631, 1388, -70, 4216, 1789, 1789, -3121,
0, 3960, 2665, -727, 1551, 0, 0, 1544, -4000, -567, 212, 212, -186,
-1243, 0, 0, -3411, -3411, -3411, 4965, -4064, -456, 4987, 4987,
2007, 2007, -190, -1634, -1634, 1537, 689, -3040, -844, -3000, 367,
-1076, -1076, 4581, -2278, -1899, -3602, -1449, -4000, -894, 985,
-957, 4384, -3149, -3747, 2391, 3940, 0, 0, 1487, -201, 3961, 3961,
-4910, 0, 0, 0, -2486, -2486, -3592, 4360, 2815, 2815, -1664,
-1664, 3135, -4170, 0, 0, 504, -2385, 0, 0, -354, -1862, -1035,
231, 231, 231, 0, -1034, 4996, 336, 2447, 0, 2563, -1115, -1115,
-357, -143, -3451, -3451, 0, -857, -857, -2057, -134, -1162, -4728,
-4728, -4728, 0, 3608, 3608, 3608, 0, 0, 0, 2559, 1068, -2393,
-563, 4619, 4619, -174, -174, -2133, -1469, 2501, -2875, 0, 910,
-235, 0, 0, 0, -4155, 0, 3526, 2167, -1872, 4703, 3555, 3555, 0,
-1864, 0, -2865, -2765, 1606, -543, 3742, 3742, -932, 4973, 1684,
0, 4239, 3537, 787, 3399, 3399, 3399, 3399, 2149, -1940, -478,
2748, 4713, 2538, 3542, 2925, 3257, -2152, -2152, -3643, -3643,
1381, -4229, -1835, 32, 32, 396, 0, };

public static void main(String[] args) {
System.out.println("howManyTimesIsFirstGreaterThanSecond() = "
+ howManyTimesIsFirstGreaterThanSecond(first, second));
System.out.println("howManyTimesDoNumbersIncrease() = "
+ howManyTimesDoNumbersIncrease(first));
System.out.println("averageOfAllEvenNumbers() = "
+ averageOfAllEvenNumbers(second));
System.out.println("firstIndexOfLongestRepeatingInteger() = "
+ firstIndexOfLongestRepeatingInteger(first, 0));
int[] subsequence = new int[] { 2398, -1388, 4676, 2958, 2958, 971, 3491,
3491, 3793, 531, 0, -1278, -3552 };
System.out.println("subsequenceStart() = "
+ subsequenceStart(first, subsequence));
}

// easy
// returns the number of times first[i] > second [i]
// you can assume first.length == second.length
public static int howManyTimesIsFirstGreaterThanSecond(int[] first, int[] second){
// implement
int count=0;
for(int i=0;i<first.length;i++){
if(first[i]>second[i]){
count++;
}
}
return count;
}

// easy
// returns the number of times is input[i + 1] > input[i]
public static int howManyTimesDoNumbersIncrease(int[] input) {
// implement
int times=0;
for(int i=0;i<input.length;i++){
if(input[i+1]>input[i]){
times++;
}
}
return times;
}

// medium
// returns the average of all even numbers
public static double averageOfAllEvenNumbers(int[] input) {
// implement
int sum=0;
for (int i=0; i<input.length;i++){
if(input[i]%2==0){
sum+=input[i];
}
}
return (double)sum/input.length;
}

// hard
// returns the first index of the longest repeating sequence of x in input
// for example, if input = {0,0,1,1,1,0,1} and x = 1, then output = 2
public static int firstIndexOfLongestRepeatingInteger(int[] input, int x) {
// implement
return 1;
}

// hard
// returns the first index where the subsequence is found in sequence
// you can assume subsequence.length < sequence.length
// you can assume subsequence is in sequence
public static int subsequenceStart(int[] sequence, int[] subsequence) {
// implement
return 1;
}
}

最佳答案

for(int i=0;i<input.length;i++){
if(input[i+1]>input[i]){
times++;
}
}

您必须在此处添加测试,因为 input[i+1] 当然超出范围。

例如:

输入长度=1000;input[999 + 1] 超出范围,因为数组的最后一个索引是 999

这是您必须添加的代码

    for(int i=0;i<input.length;i++){
if (i == input.length - 1) break;
if(input[i+1]>input[i]){
times++;
}
}

测试完所有索引后,您将退出循环。

编辑:为了清楚起见,我在可以更改循环中的条件时放置了这个示例。我选择这个是因为它更能说话。

这是另一种解决方案

for (int i = 0 ; i < input.length() - 1 ; i++){
(...)
}

关于java - 为什么当我在 Eclipse 上运行代码时会保留异常消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32670966/

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