c# - High Scores program using an array -
the assignment have user enter 10 sets of initials , 10 sets of scores , store them in array named 'player'.
below show code have created.
my problem @ moment when print array console window, shows last set of scores , initials entered.
i've tried various options array store ten sets, i'm having difficulty.
problem #1: can store 1 set of initials , scores in array. need 10 sets.
problem #2: 1 set printed, , last ones entered.
using system; using system.collections.generic; using system.linq; using system.text; namespace highscores { class program { static void main(string[] args) { string ninitials; int nscore; int counter = 0; { console.write("please enter initials:"); ninitials = convert.tostring(console.readline()); console.write("please enter score:"); nscore = convert.toint32(console.readline()); counter++; } while (counter <= 2); for(int counter2 = 0; counter <= 2; counter2++) { player[] myplayerarray = new player[3]; player[] myplayer = { new player(ninitials, nscore) }; foreach (var value in myplayer) { console.writeline("{0}", myplayer[ counter2 ]); } } #if debug console.readline(); #endif }//end main }//end class public class player { public string initials { get; set; } public int score { get; set; } public player(string pinitials, int pscore) { initials = pinitials; score = pscore; } public override string tostring() { return string.format("{0}, {1}", score, initials); } }//end class player }//end namespace
from code snippet pasted below, it's pretty obvious whenever read initials/score set console discarding previous result. need add line of code after nscore = line creates new player object , stores in array, otherwise values of ninitials , nscore discarded next time loop runs.
{ console.write("please enter initials:"); ninitials = convert.tostring(console.readline()); console.write("please enter score:"); nscore = convert.toint32(console.readline()); counter++; } while (counter <= 2);
Comments
Post a Comment