C# user input to set size of array and loop array to display elements -
the goal create array size user input via how many test scores. create loop populate array prompting user each test score 0 100. display results, typically using loop.
question: why when test scores entered example "50" adds 50 elements of 0 array?
any assistance grateful, thank you. i've seen few similar posts couldn't resolve issue. also, 1 in spanish.
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using static system.console; namespace consoleapp3 { class program { static void main(string[] args) { // prompt user ask how many test scores build size of array write("how many test scores total: "); string ssize = readline(); int = convert.toint32(ssize); int[] score = new int[i]; // create loop of asking test scores limited array ssize (int = 1; < + 1; a++) { write("please enter test score " + + " 0 100: "); string testarray = readline(); int g = convert.toint32(testarray); int[] tests = new int[g]; //create loop display test scores foreach (var item in tests) console.writeline(item); } } } }
because create new array inside of loop size of "score" user entered , loop on it. values 0 because when create array populated default value of type, in case 0. second loop should after first 1 , shouldn't creating arrays inside of first loop, populating original array (score
) created begin with.
here's want. note should index starting @ 0 , not 1.
write("how many test scores total: "); string ssize = readline(); int = convert.toint32(ssize); int[] score = new int[i]; // create loop of asking test scores limited array ssize (int = 0; < i; a++) { write("please enter test score " + (a + 1) + " 0 100: "); string testarray = readline(); int g = convert.toint32(testarray); score[a] = g; } //create loop display test scores foreach (var item in score) console.writeline(item);
you may want consider using int.tryparse
can determine if user enters invalid value.
Comments
Post a Comment