c# - Storing integers from Console to an array -
im writing program asks console x numbers console. if pick number 4, 4 different numbers should stored. program must store these inputed numbers in array, , add numbers , print out in console.
so, tried do:
console.writeline("write out number: "); int[] x = int[].parse(console.readline());
and apparently cant read in array elements console on way, need store them inside variabel , add them new array?
console.writeline("enter number of numbers add: "); //yes, know should validation here var numofnumberstoadd = int.parse(console.readline()); int value; int[] arrayvalues = new int[numofnumberstoadd]; for(int = 0; < numofnumberstoadd; i++) { console.writeline("please enter value: "); //primed read var isvalidvalue = int.tryparse(console.readline(), out value); while(!isvalidvalue) //loop until value { console.writeline("invalid value, please try again: "); //tell user why entering value again... isvalidvalue = int.tryparse(console.readline(), out value); } arrayvalues[i] = value; //store value in array } //i rather linq , lists, question asked arrays. var sum = 0; foreach(var v in arrayvalues) { sum += v; } console.writeline("sum {0}", sum);
Comments
Post a Comment