Brent
❔ Help Serializing Dictionary
Because there are only 3 'Privilege' levels, I have a new idea: I can just create 3 different normal lists and save them each in a new file, one list per Privilege level. The list contains all the 'Manager' instances that have that specific privilege level. School sucks, this could be much easier but yeah
14 replies
❔ hw help
using System;
public class Program { public static int CountEven(int num) { int digit; int evencount = 0; while (num != 0) { digit = num % 10; if (digit % 2 == 0) // changed here { evencount++; } num = num/10; } return evencount; } public static void Main() { int n; Console.WriteLine("Write how many numbers are there?"); n = int.Parse(Console.ReadLine()); int Max = 0; // added here int MaxNum = 0; // added here for (int i = 1; i <= n; i++) { int c = int.Parse(Console.ReadLine()); int evenCount = CountEven(c); Console.WriteLine("This number has this much even numbers: " + evenCount); if (evenCount > Max) // changed here { Max = evenCount; MaxNum = c; // added here } } Console.WriteLine("The number with the most even numbers is: " + MaxNum + " with " + Max + " even numbers."); // added here } }
7 replies
❔ hw help
So you should actually just store the number that has the most even numbers in Max, and always check if the new number has more even numbers or not, if that's the case, overwrite it.
Only in the end (after all of the numbers were checked for the amount of even numbers) the Max should be printed
7 replies
❔ hw help
The issue with your code is that the Console.WriteLine("This number has the most even numbers:" + Max); statement is inside the for loop, so it will be executed multiple times. As a result, the last number that has the most even numbers will be printed multiple times. To fix this, you can move the Console.WriteLine("This number has the most even numbers:" + Max); statement outside of the for loop so that it is only executed once. Additionally, you should initialize the Max variable to the first number's even count instead of 0, so that the first number is always considered as a candidate for having the most even numbers.
7 replies