c# - How do I split an array into two different arrays? -
i can't seem figure out how fix code works. need user able input first name space scored. need split array 2 different arrays , pass them 4 different methods display user scored, etc. can me figure problem out?
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace proj09lea { class program { static void main(string[] args) { // declare , array of integers int[] array = new int[10]; console.writeline("\nsaturday coder's bowling team"); console.writeline("enter in name , score each person on team."); console.writeline("for example, mary 143. hit enter when done.\n"); // fill array user input (int = 0; < array.length; i++) { console.writeline("enter in name , score: "); string userinput; string[] parsedinput; parsedinput = userinput.split(); string name = parsedinput[0]; int score = int.parse(parsedinput[1]); } console.writeline("------------ input complete ------------\n"); console.writeline("here scores game:"); displayscore(array); highscore(array); lowscore(array); averagescore(array); console.writeline("press enter continue. . ."); console.readline(); } static void displayscore(int[] array) { foreach (int n in array) { console.writeline("{0}'s score {0}.\n", array); } } static void highscore(int[] array) { int max = array.max(); console.writeline("congratulations {0}, score of {0} highest.", max); } static void lowscore(int[] array) { int min = array.min(); console.writeline("{0}, score of {0} lowest. better practice.", min); } static void averagescore(int[] array) { int sum = array.sum(); int average = sum / array.length; console.writeline("the average score game {0:d}.", average); } } }
if absolutely have use simple primitive arrays, need 2 distinct arrays of same size, hold names strings , scores ints:
class program { const int maxscores = 10; // .. use constant ensure sizes remain in sync static void main(string[] args) { /// string[] names = new int[maxscores]; int[] scores = new int[maxscores]; // ... parse names names[] , scores scores[] displayscore(names, scores);
you need pass both arrays various methods:
static void displayscore(string[] names, int[] scores) { for(int i=0; < maxscores; i++) { console.writeline("{0}'s score {1}.\n", names[i], scores[i]); } } // etc
however, there better ways this, e.g. defining custom class tuple of name, score
:
class personscore { public string name {get; set;} public int score {get; set;} }
you can declare , pass single array of personscore[]
around.
personscore[] personscores = new personscore[maxscores]; (... prompting user data) { ... parsing user input personscores[i] = new personscore{name = name, score = score}; } displayscore(personscores); // pass around single array static void displayscore(ienumerable personscores) { foreach(var personscore in personscores) { console.writeline("{0}'s score {1}.\n", personscore.name, personscores.score); } } // etc - other methods
as others have mentioned, other collections possible alternatives array, commonly list
.
Comments
Post a Comment