Program shuts down when array is full, Need a search and sort too
我正在尝试设置我的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace sodacrate { class Bottles //skapar klassen Soda f?r att samla information om inneh?llet i backens SMAK, PRIS och av vilken TYP drickan ?r (vatten eller l?sk) s.134-> { string flavor; //{"Cola","Water","Orange","Raspberry","GrapeTonic" } int price; // 4, 5, 6, 7, 8 //METOD: CONSTRUCTOR public Bottles(string flavor, int price) { this.flavor = flavor; this.price = price; } //Egenskap f?r flavor public string Flavor { get { return flavor; } set { flavor = value; } } //Egenskap f?r price public int Price { get { return price; } set { price = value; } } public override string ToString() { return string.Format(Flavor +"" + Price); //return string.Format("The bottle {0} costs {2} G.", flavor, price); } } class Sodacrate { Bottles[] myCrate = new Bottles[25]; //create empty array that holds 25 string[] flavors = new string[25]; //create empty list of current flavors in crate int[] prices = new int[25]; //create empty list of current prices in crate //List<string> flavors = new List<string>(); //create empty list of current flavors in crate //List<int> prices = new List<int>(); private int amountBottles = 0; //Identifierare. H?ller reda p? antal flaskor public int crateValue = 0; //Sammanlagda v?rdet p? alla flaskor som finns i backen public void Run() { int temp = 0; do { Console.Clear(); Console.WriteLine(" "); Console.WriteLine("*********************************************"); Console.WriteLine("** Welcome to your Sodacrate! **"); Console.WriteLine("*********************************************"); Console.WriteLine("* *"); Console.WriteLine("* These are your options: *"); Console.WriteLine("* *"); Console.WriteLine("* 1. Add soda to your crate *"); Console.WriteLine("* 2. Print the content *"); Console.WriteLine("* 3. Calculate the content *"); Console.WriteLine("* 4. Sort sodas *"); Console.WriteLine("* 5. Find sodas *"); Console.WriteLine("* 6. Remove bottles *"); Console.WriteLine("* 0. Quit *"); Console.WriteLine("* *"); Console.WriteLine("* *"); Console.WriteLine("*********************************************"); Console.WriteLine(" "); temp = int.Parse(Console.ReadLine()); switch (temp) { case 1: add_soda(); break; case 2: print_crate(); break; case 3: calc_total(); break; case 4: sort_sodas(); break; case 5: find_soda(); break; case 6: remove_soda(); break; case 0: Console.WriteLine("Shutting down program"); //avsluta programmet. break; default: Console.WriteLine("Incorrect Input"); //skrivs ut om annat ?n en siffra mellan 0-6 anges. break; } } while (temp != 0); } public void add_soda() { int adding = 0; do { //Console.Clear(); //tar bort all f?reg?ende text i konsolf?nstret //menyn f?r att v?lja smak Console.WriteLine(" "); Console.WriteLine("*****************************************************"); Console.WriteLine("** Which flavor do you like? **"); Console.WriteLine("*****************************************************"); Console.WriteLine("* *"); Console.WriteLine("* Choose by selecting 1-5 and ENTER or 0 to go back *"); Console.WriteLine("* *"); Console.WriteLine("* 1. COLA. Costs 4 G *"); Console.WriteLine("* 2. WATER. Costs 5 G *"); Console.WriteLine("* 3. ORANGE. Costs 6 G *"); Console.WriteLine("* 4. RASPBERRY Costs 7 G *"); Console.WriteLine("* 5. GRAPE TONIC Costs 8 G *"); Console.WriteLine("* 0. Return to Main Menu *"); Console.WriteLine("* *"); Console.WriteLine("*****************************************************"); Console.WriteLine(" "); adding = int.Parse(Console.ReadLine()); //sj?lva valen, input 0-5 och sen ENTER f?r att verkst?lla if (amountBottles >= 25) { Console.WriteLine(" - Your crate is full!"); Console.WriteLine(amountBottles); } else { switch (adding) { case 1: Bottles Cola = new Bottles("Cola", 4); myCrate[amountBottles] = Cola; Console.WriteLine("Cola"); break; case 2: Bottles Water = new Bottles("Water", 5); myCrate[amountBottles] = Water; Console.WriteLine("Water"); break; case 3: Bottles Orange = new Bottles("Orange", 6); myCrate[amountBottles] = Orange; Console.WriteLine("Orange"); break; case 4: Bottles Raspberry = new Bottles("Raspberry", 7); myCrate[amountBottles] = Raspberry; Console.WriteLine("Raspberry"); break; case 5: Bottles GrapeTonic = new Bottles("GrapeTonic", 8); myCrate[amountBottles] = GrapeTonic; Console.WriteLine("Grape Tonic"); break; default: Console.WriteLine("Incorrect Input"); break; } amountBottles++; } }while (adding != 0); } public void print_crate() { int keepshopping1 = 0; do { amountBottles--; //removes the extra unidentified bottle that always ends up in the crate when calling upon add_soda Console.Clear(); Console.WriteLine("*******************************************************"); Console.WriteLine("** Contents of your Soda Crate **"); Console.WriteLine("*******************************************************"); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Purchase more bottles? " +"[1] to Purchase, [2] to Remove bottles or [0] to go back to Main Menu."); Console.WriteLine(" "); Console.WriteLine("Amount of bottles in your crate:" + amountBottles ); int i = 0; //counting variable while (myCrate[i] != null) //counts while no element in myCrate is null { string temp = myCrate[i].Flavor; // gets the"name" property of the object flavors[i] = temp; //flavors.Add(temp); //adds the name property to the list"flavors" -LIST-funktionen i++; } var a = from x in flavors //orders and counts duplicates in list group x by x into g let count = g.Count() orderby count descending select new { Value = g.Key, Count = count }; foreach (var x in a) Console.WriteLine(x.Value +"" + x.Count +" bottles"); //prints sorted, grouped list keepshopping1 = int.Parse(Console.ReadLine()); switch (keepshopping1) { case 1: add_soda(); break; case 2: remove_soda(); break; case 0: //tillbaka till huvudmenyn break; default: Console.WriteLine("Incorrect Input"); //skrivs ut om annat ?n en siffra 1,2 eller 0 anges. break; } } while (keepshopping1 != 0); } public void calc_total() { int sum = 0; int keepshopping2 = 0; do { Console.Clear(); Console.WriteLine("*******************************************************"); Console.WriteLine("** Cost of your Soda Crate **"); Console.WriteLine("*******************************************************"); Console.WriteLine(); int i = 0; //counting variable crateValue = sum; while (myCrate[i] != null) //counts while no element in myCrate is null { sum = sum + myCrate[i].Price; i++; } Console.WriteLine("This will be" + sum +" G's, sir."); Console.WriteLine(" "); Console.WriteLine("Continue shopping? " +"[1] to Continue, [2] to Remove soda or [0] to go back to Main Menu."); Console.WriteLine(" "); keepshopping2 = int.Parse(Console.ReadLine()); switch (keepshopping2) { case 1: add_soda(); break; case 2: remove_soda(); break; case 0: //tillbaka till huvudmenyn break; default: Console.WriteLine("Incorrect Input"); //skrivs ut om annat ?n siffra 1,2 eller 0 anges. break; } } while (keepshopping2 != 0); //T?nk p? att inte r?kna med tomma positioner i vektorn } public void find_soda() { } public void sort_sodas() { int max = myCrate.Length - 1; //outer loop: Goes through the entire list until everything's sorted for (int m = 0; m < max; m++) { //inner loop: Goes through every element and comparing them to eachother. Doesn't go through what's already sorted. int sorted = max - m; //to see how many has been gone through for (int n = 0; n < sorted; n++) { if (myCrate[n] > myCrate[n+1]) //comparing elements ERROR cs0019 { //switch place int temp3 = myCrate[n]; myCrate[n] = myCrate[n+1]; myCrate[n+1] = temp3; } } } //write the list for (int m = 0; m < myCrate.Length; m++) Console.WriteLine(myCrate[m]); } public void remove_soda() { if (myCrate == null) { Console.WriteLine("Your crate is empty."); Console.ReadKey(); return; } Console.WriteLine("Name on the bottle:"); string name = Console.ReadLine(); if (myCrate.Select(x => x.Flavor).Contains(flavors)) //errorcs1929 { var itemToRemove = myCrate.Where(x => x.Flavor.Equals(flavors)).First(); if (itemToRemove != null) myCrate.Remove(itemToRemove); //error 1061 - 'Bottles[]' cannot contain a definition for 'Remove' } else { Console.WriteLine("Name not found."); Console.ReadKey(); } } } class Program { public static void Main(string[] args) { //Skapar ett objekt av klassen Sodacrate som heter Sodacrate var Sodacrate = new Sodacrate(); Sodacrate.Run(); Console.Write("Press any key to continue . . ."); Console.ReadKey(true); } } } |
编辑
我试过用这种方法除去苏打水,但我不能把我的头挪到哪里出错了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | if (myCrate == null) { Console.WriteLine("Your crate is empty."); Console.ReadKey(); return; } Console.WriteLine("Name on the bottle:"); string name = Console.ReadLine(); if (myCrate.Select(x => x.Flavor).Contains(flavors)) { var itemToRemove = myCrate.Where(x => x.Flavor.Equals(flavors)).First(); if (itemToRemove != null) myCrate.Remove(itemToRemove); } else { Console.WriteLine("Name not found."); Console.ReadKey(); } |
这是我的高贵尝试,在某种程度上实现泡沫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public void sort_sodas() { int max = myCrate.Length - 1; //outer loop: Goes through the entire list until everything's sorted for (int m = 0; m < max; m++) { //inner loop: Goes through every element and comparing them to eachother. Doesn't go through what's already sorted. int sorted = max - m; //to see how many has been gone through for (int n = 0; n < sorted; n++) { if (myCrate[n] > myCrate[n+1]) //comparing elements { //switch place int temp3 = myCrate[n]; myCrate[n] = myCrate[n+1]; myCrate[n+1] = temp3; } } } //write the list for (int m = 0; m < myCrate.Length; m++) Console.WriteLine(myCrate[m]); } |
但是,它给了我3个错误。
1:运算符">"不能应用于"瓶子"和"瓶子"类型的操作数。
2:无法将类型"sodacrate.bottles"隐式转换为"int"
3:无法将类型"int"隐式转换为"sodacrate.bottles"
所以如果我小于25,它就永远不等于25
数组是为固定不变的数据而设计的,我建议使用列表
1 |
然后你可以做
1 2 3 4 5 6 7 8 | if (Crate.Count >= 25) { Console.WriteLine(" - Your crate is full!"); } else { Crate.Add(bottle); } |
对于分组结果,Linq是您的朋友
1 | crate.Groupby(b=>b.Flavor).Select(g=>g.Count() +"" + g.Key); |
将返回可枚举的字符串
你也可以用Linq的
1 | crate.Orderby(b=>b.Price) |
也可以使用Linq的
1 | crate.Where(b=>b.Flavour == SearchField) |
编辑:更明确一点添加函数应该如下所示
1 2 3 4 5 6 7 8 9 10 11 | adding = int.Parse(Console.ReadLine()); if (amountBottles >= 25) { Console.WriteLine(" - Your crate is full!"); } else { //Your Switch statement here amountBottles++; } |
你的指纹应该是
1 2 3 4 5 6 7 8 9 10 11 12 | for (int i = 0; i < myCrate.Length; i++) { if (myCrate[i] != null) { Console.WriteLine( myCrate[i].Flavor ); } else { Console.WriteLine("Empty Space"); } } |
编辑:就像你想用out-linq进行分组和排序一样然后,对于分组,您需要创建一个组类,该组类将保存您的瓶子类型和计数,同时生成总数s,然后使用该组进行打印。
对于排序,那么您需要实现一个排序算法,最好是快速排序,尽管插入排序更简单。