using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ohjaus1 { class Program { static void Main(string[] args) { int l1 = 1; int l2 = 1; while (l1 != 0 || l2 != 0) { kysy(ref l1, ref l2); //viedään parametrit referenssinä, että saadaan luetut luvut käyttöön kutsuvassa ohjelmassa Console.WriteLine("Lukujen " + l1.ToString() + " ja " + l2.ToString() + " summa on " + summa(l1, l2).ToString()); } Console.WriteLine("Sama toiminta käyttäen omaa luokkaa"); Numerot numerot = new Numerot(); numerot.Kysy(); Console.WriteLine("Sama toiminta mutta kertolaskuluokalla"); Numerot kertolasku = new Kertolasku(); kertolasku.Kysy(); } static int summa(int luku1, int luku2) { return luku1 + luku2; } static void kysy(ref int luku1, ref int luku2) { try { Console.Write("Anna kokonaisluku: "); string l1 = Console.ReadLine(); luku1 = Convert.ToInt16(l1); } catch (Exception) { Console.WriteLine("Annoit epäkelvon numeron"); luku1 = 0; } try { Console.Write("Anna kokonaisluku: "); string l2 = Console.ReadLine(); luku2 = Convert.ToInt16(l2); } catch (Exception) { Console.WriteLine("Annoit epäkelvon numeron"); luku2 = 0; } } } class Numerot { protected int l1 = 1; // pitää olla protected niin peritty luokka pääsee suoraan käsiksi protected int l2 = 1; public virtual void Kysy() { while (l1 != 0 || l2 != 0) { kysy(); Console.WriteLine("Lukujen " + l1.ToString() + " ja " + l2.ToString() + " summa on " + laske().ToString()); } } int laske() { return l1 + l2; } protected void kysy() { try { Console.Write("Anna kokonaisluku: "); string s1 = Console.ReadLine(); l1 = Convert.ToInt16(s1); } catch (Exception) { Console.WriteLine("Annoit epäkelvon numeron"); l1 = 0; } try { Console.Write("Anna kokonaisluku: "); string s2 = Console.ReadLine(); l2 = Convert.ToInt16(s2); } catch (Exception) { Console.WriteLine("Annoit epäkelvon numeron"); l2 = 0; } } } class Kertolasku : Numerot { public override void Kysy() { while (l1 != 0 || l2 != 0) { kysy(); Console.WriteLine("Lukujen " + l1.ToString() + " ja " + l2.ToString() + " tulo on " + laske().ToString()); } } int laske() { return l1 * l2; } } }