Hello,
How can i give an error depending on the array input? This is my code
So im going to give an error if the user inputs different number besides 1-99, please help you guys, i've never done any error coding.
How can i give an error depending on the array input? This is my code
Code:
using System; class MainClass { public static void Main (string[] args) { int[] randomSizedArray; string sizeOfArray; int convertedSizeArray = -1; Console.WriteLine ("Please Enter the Size of the Array Between 1-99"); sizeOfArray = Console.ReadLine(); convertedSizeArray = Int32.Parse(sizeOfArray); randomSizedArray= new int[convertedSizeArray]; Random rnd = new Random(); for (int i=0; i < convertedSizeArray; i++) { randomSizedArray[i] = rnd.Next(1,99); } for (int i=0; i < convertedSizeArray; i++) { Console.WriteLine(randomSizedArray[i] + ""); } string swapindex1; string swapindex2; int index1; int index2; Console.WriteLine("Please Enter Index to swap"); swapindex1 = Console.ReadLine(); index1 = Int32.Parse(swapindex1); int temp = randomSizedArray[index1]; Console.WriteLine ("Please Enter a Second Value to swap"); swapindex2 = Console.ReadLine(); index2 = Int32.Parse(swapindex2); randomSizedArray[index1] = randomSizedArray[index2]; randomSizedArray[index2] = temp; for (int j=0; j < convertedSizeArray; j++){ Console.WriteLine(randomSizedArray[j]); } } }
Comment