I need to be able to print out the temperature array with user input and then print it out unsorted, and then copy that array from waterDepths at index 1 the next three values starting in array w at index 5 unsorted, sorted and reversed with the w array, but every time it runs I have two problems:
- the length exception but I don't understand why that happens
- before the exception it prints out the original array at index 0, and then it reprints for 0 and 1, and over and over until it's done it for the length of the array if that makes sense.
This is the error I run into
Unhandled Exception:
System.ArgumentException: length
using System;
class MainClass
{
static void Main ()
{
Console.Write("how many depths do you want to enter?: ");
int depths = Convert.ToInt32(Console.ReadLine());
int [] temperature = new int [depths];
int [] w = new int [depths+5];
InputValues(temperature);
// array w will reference the same array
// as the temperature array
w = temperature;
DisplayOutput(w, "waterDepth Array
");
Array.Copy(temperature, 1, w, 5, 4);
Console.WriteLine("Array w Not Sorted");
Array.Sort(w);
DisplayOutput(w, "array w sorted
");
Array.Reverse(w);
DisplayOutput(w, "array w reversed
");
} // end Main
public static void InputValues(int [] temp)
{
string inValue;
for(int i = 0; i< temp.Length; i++)
{
Console.Write("enter Temperature {0}: ", i+1);
inValue = (Console.ReadLine());
temp[i] = Convert.ToInt32(inValue);
} // end for
} // end input
public static void DisplayOutput(int [] anArray,string msg)
{
foreach(double wVal in anArray)
{
if (wVal>0)
msg +=wVal + "
";
} // end foreach
} //end disp
}//end class
question from:
https://stackoverflow.com/questions/66059366/unhandled-length-exception-halfway-through-program 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…