C # exercise analysis after programming and development,
C # Analysis of exercises after programming and development.
5.3 case 2
Use an array to store the scores of 10 students, calculate the total score and average score, and output
using System;?namespace boring{ ? ?class MainClass ? {? ? ? ? ?public static void Main(string[] args) ? ? ? { ? ? ? ? ? ?double sum = 0; ? ? ? ? ? ?double ave; ? ? ? ? ? ?int[] myArray = new int[10]; ? ? ? ? ? ?for (int i = 0; i != 10; i ++) { ? ? ? ? ? ? ? ?myArray[i] = int.Parse(Console.ReadLine()); ? ? ? ? ? } ? ? ? ? ? ?for (int i = 0; i != 10; i++) { ? ? ? ? ? ? ? ?sum = sum + myArray[i]; ? ? ? ? ? } ? ? ? ? ? ?ave = sum / 10; ? ? ? ? ? ?Console.WriteLine(sum); ? ? ? ? ? ?Console.WriteLine(ave); ? ? ? }? ? }}?
5.3 cases 3
Use ArrayList to store the scores of 10 students, add the total score of all students in the last item, delete the scores below 60, and output all the elements of ArrayList
using System;using System.Collections;?namespace Application{ ? ?class MainClass ? { ? ? ? ?public static void Main(string[] args) ? ? ? { ? ? ? ? ? ?double sum = 0; ? ? ? ? ? ?ArrayList arraylist = new ArrayList(11); ? ? ? ? ? ?for (int i = 0; i != 10; i++) { ? ? ? ? ? ? ? ?double t = int.Parse(Console.ReadLine()); ? ? ? ? ? ? ? ?arraylist.Add(t); ? ? ? ? ? ? ? ?sum = sum + t; ? ? ? ? ? } ? ? ? ? ? ?arraylist.Add(sum); ? ? ? ? ? ?foreach(double p in arraylist) { ? ? ? ? ? ? ? ?if (p > 60) ? ? ? ? ? ? ? ?Console.Write(p + " ? "); ? ? ? ? ? } ? ? ? } ? }}?
5.3 cases 4
Use Hashtable to store the scores of 10 students. Use name as the key and score as the value. Obtain the total score and add it to Hashtable.
using System;using System.Collections;?namespace Application{ ? ?class MainClass ? { ? ? ? ?public static void Main(string[] args) ? ? ? { ? ? ? ? ? ?double sum = 0; ? ? ? ? ? ?Hashtable tab = new Hashtable(11); ? ? ? ? ? ?for (int i = 0; i != 10; i++) { ? ? ? ? ? ? ? ?string y = Console.ReadLine(); ? ? ? ? ? ? ? ?int x = int.Parse(Console.ReadLine()); ? ? ? ? ? ? ? ?sum = sum + x; ? ? ? ? ? ? ? ?tab.Add(y, x); ? ? ? ? ? } ? ? ? ? ? ?tab.Add("sum", sum); ? ? ? ? ? ?foreach (DictionaryEntry i in tab) { ? ? ? ? ? ? ? ?Console.WriteLine(i.Value); ? ? ? ? ? } ? ? ? } ? }}?