Write the following code in c #1. Write a method with the following specifications: name: DisplayMenu arguments: none return value: none tasks: display the following menu choice on the screen Calculation Menu, developed by StudentID:xxxx B) Calculate Number E) To Exit Enter the Letter that corresponds to your choice: Please update your ID in the menu. You don’t have to implement the functionalities of the various menu choices at this stage Call this method from your main. 2. Modify your main so that the above method is call repeatedly. The program will terminate when the user enters E. Any other choice should produce an error message. Because you will not be doing any arithmetic you may accept the user response either as an int, or a char or a string. 3. Write a method with the following specifications: name: CalculateN arguments: an int array with values ranging from 0 to 10 return value: int array of 3 integers. The first element will indicate the total sum of all the values in the array, the second element will indicate the average of all the values in the array, the third element will indicate the smallest value in the array. displays: none tasks: The method will create an int array of 3 elements (call this the result). In your main you will generate an int array of 5 elements with values ranging from 0 to 10, call this method and display the resulting array’s value

时间: 2024-01-31 13:02:06 浏览: 28
Sure, here's the code in C#: ```csharp using System; class Program { static void Main(string[] args) { char choice; int[] arr = new int[] { 5, 2, 9, 7, 3 }; do { DisplayMenu(); choice = Console.ReadKey().KeyChar; Console.WriteLine(); switch (choice) { case 'B': int[] result = CalculateN(arr); Console.WriteLine($"Total sum: {result[0]}"); Console.WriteLine($"Average: {result[1]}"); Console.WriteLine($"Smallest value: {result[2]}"); break; case 'E': Console.WriteLine("Exiting..."); break; default: Console.WriteLine("Invalid choice. Please try again."); break; } Console.WriteLine(); } while (choice != 'E'); } static void DisplayMenu() { Console.WriteLine("Calculation Menu, developed by StudentID:xxxx"); Console.WriteLine("B) Calculate Number"); Console.WriteLine("E) To Exit"); Console.Write("Enter the Letter that corresponds to your choice: "); } static int[] CalculateN(int[] arr) { int totalSum = 0; int smallestValue = arr[0]; foreach (int num in arr) { totalSum += num; if (num < smallestValue) { smallestValue = num; } } int average = totalSum / arr.Length; return new int[] { totalSum, average, smallestValue }; } } ``` Explanation: - We start by declaring a `char` variable called `choice` to store the user's menu choice, and an `int` array called `arr` with 5 elements and some sample values. - In the `Main` method, we use a `do-while` loop to repeatedly display the menu and prompt the user for a choice, until they enter 'E' to exit. - Inside the loop, we use a `switch` statement to check the user's choice and perform the appropriate action. If they choose 'B', we call the `CalculateN` method with the `arr` array as argument, and display the resulting array's values. If they choose 'E', we simply print a message and the loop will exit. - The `DisplayMenu` method simply prints the menu to the console. - The `CalculateN` method takes an `int` array as argument, and returns another `int` array with 3 elements: the total sum, the average, and the smallest value. We use a `foreach` loop to iterate over the array and calculate these values, and then return them as an array.

相关推荐

最新推荐

recommend-type

解决python脚本中error: unrecognized arguments: True错误

主要介绍了解决python脚本中error: unrecognized arguments: True错误,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

yolov5-face-landmarks-opencv

yolov5检测人脸和关键点,只依赖opencv库就可以运行,程序包含C++和Python两个版本的。 本套程序根据https://github.com/deepcam-cn/yolov5-face 里提供的训练模型.pt文件。转换成onnx文件, 然后使用opencv读取onnx文件做前向推理,onnx文件从百度云盘下载,下载 链接:https://pan.baidu.com/s/14qvEOB90CcVJwVC5jNcu3A 提取码:duwc 下载完成后,onnx文件存放目录里,C++版本的主程序是main_yolo.cpp,Python版本的主程序是main.py 。此外,还有一个main_export_onnx.py文件,它是读取pytorch训练模型.pt文件生成onnx文件的。 如果你想重新生成onnx文件,不能直接在该目录下运行的,你需要把文件拷贝到https://github.com/deepcam-cn/yolov5-face 的主目录里运行,就可以生成onnx文件。
recommend-type

setuptools-0.6c8-py2.5.egg

文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

5-3.py

5-3
recommend-type

Java八股文.pdf

"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它指的是一系列在Java编程面试中经常被问到的基础知识点、理论概念和技术细节。这个术语的命名来源于中国古代科举考试中的“八股文”,一种具有固定格式和套路的文章形式。 在Java编程的上下文中,"Java八股文"通常包括以下几个方面:"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它指的是一系列在Java编程面试中经常被问到的基础知识点、理论概念和技术细节。这个术语的命名来源于中国古代科举考试中的“八股文”,一种具有固定格式和套路的文章形式。 在Java编程的上下文中,"Java八股文"通常包括以下几个方面:"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它指的是一系列在Java编程面试中经常被问到的基础知识点、理论概念和技术细节。这个术语的命名来源于中国古代科举考试中的“八股文”,一种具有固定格式和套路的文章形式。 在Java编程的上下文中,"Java八股文"通常包括以下几个方面:"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。