
なぜエラーが出るのか理解したいです。
ある、本の課題をしているときに発生したエラーです。
問題のある箇所は
intarray[i] = x;
の部分で、エラー内容は、インデックスの配列の範囲外という内容でした。
その理由がわからないので教えて欲しいです。
ついでに課題内容は、
int型とint型の値の和とdouble型とdouble型の値の和を求めるメソッドをもつクラスを、オーバーロードを利用して作り、またMainメソッドからこれらのメソッドを利用して和を求めなさい。
という内容でした。
そこで、課題の内容が簡単だったので、工夫して以下の内容に挑戦して見ました。
入力した値をint型とint型の和、もしくはdouble型とdouble型の値を入力し、和を求めるメソッドを持つクラスを、オーバーロードを利用して作り、さらに、thisを利用し、Mainメソッドからこれらのこれらのメソッドを利用して和を求めるというコードに挑戦しました。
が失敗してしまいました。
その上、読みにくいコードにもなってしまいました。
読みやすく、わかりやすいコードにもしたいので、アトバイスを出来れば教えて頂けないでしょうか?
よろしくお願いします。
発生している問題・エラーメッセージ
System.IndexOutOfRangeException: "Index was outside the bounds of the array."
該当のソースコード
lang
1using System; 2 3class Overload 4{ 5 6 public void Show() 7 { 8 var ans = this; 9 Console.WriteLine("ans = {0}", ans); 10 } 11 12 public Overload(int x, int y) 13 { 14 int ans = x + y; 15 } 16 17 public Overload(double x, double y) 18 { 19 double ans = x + y; 20 } 21} 22 23class Start 24{ 25 public static void Main() 26 { 27 bool integre = false; 28 bool doublegre = false; 29 int[] intarray; 30 double[] doublearray; 31 int x = 0; 32 int y = 0; 33 double xd = 0; 34 double yd = 0; 35 36 for (int i = 0; i < 2; i++) 37 { 38 while(true) 39 { 40 Console.WriteLine("Please enter the digit"); 41 string num_str = Console.ReadLine(); 42 43 if (!Char.IsDigit(num_str[0]) && num_str[0] != '-') 44 { 45 Console.WriteLine("Incrrect input"); 46 Console.WriteLine(); 47 continue; 48 } 49 if (integre == false && doublegre == false) 50 { 51 while (true) 52 { 53 Console.WriteLine("int or double ?(1 or 2)"); 54 string select = Console.ReadLine(); 55 56 bool select_roop = false; 57 58 switch (select) 59 { 60 case "1": 61 Console.WriteLine("You selected 1"); 62 integre = true; 63 select_roop = true; 64 break; 65 66 case "2": 67 Console.WriteLine("You selected 2"); 68 doublegre = true; 69 select_roop = true; 70 break; 71 72 default: 73 Console.WriteLine("Incrrect input"); 74 break; 75 } 76 if (select_roop == true) 77 { 78 break; 79 } 80 } 81 if (integre == true) 82 { 83 intarray = new int[i]; 84 85 if (i == 0) 86 { 87 x = int.Parse(num_str); 88 intarray[i] = x; //Error 89 } 90 else if (i == 1) 91 { 92 y = int.Parse(num_str); 93 intarray[i] = y; 94 } 95 else 96 { 97 Console.WriteLine("Error"); 98 } 99 } 100 else if (doublegre == true) 101 { 102 doublearray = new double[i]; 103 104 if (i == 0) 105 { 106 xd = double.Parse(num_str); 107 doublearray[i] = xd; 108 } 109 else if (i == 1) 110 { 111 yd = double.Parse(num_str); 112 doublearray[i] = xd; 113 } 114 else 115 { 116 Console.WriteLine("Error"); 117 } 118 } 119 else 120 { 121 Console.WriteLine("Error!"); 122 } 123 } 124 break; 125 } 126 } 127 128 if (integre == true) 129 { 130 Overload ov = new Overload(x, y); 131 ov.Show(); 132 } 133 134 if(doublegre == true) 135 { 136 Overload ov = new Overload(xd, yd); 137 ov.Show(); 138 } 139 } 140} 141
上記のコードを直しても正常に動かないので修正後のコードを追記しました。
lang
1using System; 2 3class Overload 4{ 5 6 public void Show() 7 { 8 var ans = this; 9 Console.WriteLine("This ans = {0}", ans);//Ovreload display 10 } 11 12 public int Over(int x, int y) 13 { 14 int ans = (x + y); 15 return ans; 16 } 17 18 public double Over(double x, double y) 19 { 20 double ans = (x + y); 21 return ans; 22 } 23} 24 25class Start 26{ 27 public static void Main() 28 { 29 bool integre = false; 30 bool doublegre = false; 31 int[] intarray; 32 double[] doublearray; 33 int x = 0; 34 int y = 0; 35 double xd = 0; 36 double yd = 0; 37 38 for (int i = 0; i < 2; i++) 39 { 40 while (true) 41 { 42 Console.WriteLine("Please enter the digit"); 43 string num_str = Console.ReadLine(); 44 45 if (!Char.IsDigit(num_str[0]) && num_str[0] != '-') 46 { 47 Console.WriteLine("Incrrect input"); 48 Console.WriteLine(); 49 continue; 50 } 51 if (integre == false && doublegre == false) 52 { 53 while (true) 54 { 55 Console.WriteLine("int or double ?(1 or 2)"); 56 string select = Console.ReadLine(); 57 58 bool select_roop = false; 59 60 switch (select) 61 { 62 case "1": 63 Console.WriteLine("You selected 1"); 64 integre = true; 65 select_roop = true; 66 break; 67 68 case "2": 69 Console.WriteLine("You selected 2"); 70 doublegre = true; 71 select_roop = true; 72 break; 73 74 default: 75 Console.WriteLine("Incrrect input"); 76 break; 77 } 78 if (select_roop == true) 79 { 80 break; 81 } 82 } 83 } 84 else if (doublegre == true) 85 { 86 doublearray = new double[i]; 87 88 if (i == 0) 89 { 90 xd = double.Parse(num_str); 91 doublearray[i] = xd; 92 } 93 else if (i == 1) 94 { 95 yd = double.Parse(num_str); 96 doublearray[i] = xd; 97 } 98 else 99 { 100 Console.WriteLine("Error"); 101 } 102 } 103 else 104 { 105 Console.WriteLine("Error!"); 106 } 107 if (integre == true) 108 { 109 intarray = new int[2]; 110 111 if (i == 0) 112 { 113 x = int.Parse(num_str); 114 intarray[i] = x; 115 } 116 else if (i == 1) 117 { 118 y = int.Parse(num_str); 119 intarray[i] = y; 120 } 121 else 122 { 123 Console.WriteLine("Error"); 124 } 125 } 126 break; 127 } 128 } 129 130 if (integre == true) 131 { 132 Overload ov = new Overload(); 133 ov.Show(); 134 Console.WriteLine("ans = {0}",ov.Over(x, y)); 135 } 136 137 if (doublegre == true) 138 { 139 Overload ov = new Overload(); 140 ov.Show(); 141 Console.WriteLine("ans = {0}", ov.Over(x, y)); 142 143 } 144 } 145}


回答3件
あなたの回答
tips
プレビュー