[英]Index was outside of bounds of the array error
public class dailyMenu
{
private string day="";
private int date = 0;
private static int nextDate=1;
private string entree ="";
private double price;
private double calories;
private static string [] daysOfWeek= {"Monday","Tuesday","Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"};
private static string[] entrees = {"Beef Tenderloin Fresco",
"Madagascar Filet Mignon", "Filet Mignon", " Lobster Ravioli",
"Asian Infused Braised Beef", "New Age Chicken Cordon Bleu",
"Short Ribs", " Beef Wellington","Fajitas", "Bacon Cheeseburger",
"Beef Burgandy", "Spagehetti"};
private static double [] entreePrices= { 5.99,7.99,6.99,4.50,9.99,10.29,
5.67,8.99, 3.99,4.78,10,79,6.98};
private static int[] entreeMealCaloricVal= { 999,1288,770,699,450,999,1500,873, 911,
1011, 777,500};
public dailyMenu()
{
assignDate();
GetDay();
RandPopulate();
}
void assignDate()
{
date = nextDate;
nextDate++;
if (GetDay()== "Friday")
{
nextDate += 2;
}
}
void RandPopulate()
{
Random random = new Random();
int randomNumber = random.Next(0,13);
entree = entrees [randomNumber];
price = entreePrices [randomNumber];
calories = entreeMealCaloricVal [randomNumber];
}
}
IDE告訴我,第56、41和14行可能是問題所在,因此我猜測這與我的隨機數生成器有關。
有人可以幫我嗎?
看起來像entrees
和entreeMealCaloricVal
包含12個項目。 這意味着它們將從0索引到11。
使用random.Next(0,13)
將生成一個從0到12的數字。當您嘗試訪問索引12的那些數組時,將引發異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.