C#面向对象程序设计课程实验三:实验名称:C#数组和集合


实验目的及要求
Microsoft Visual Studio 2008

注意考虑以下情况:
①输入成绩在 0-100 的范围之外的处理。
②如果输入的成绩后面带有分号等标点符号,也可以过滤。
③当有多个相同的最高或最低分时,也能分别显示出来。
实验的程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 实验三_1_
{
class Program
{
static double Average(int[] Scores)
{
double total = 0;
for (int i = 0; i < Scores.GetLength(0); i++)
total += Scores[i];
return (double)(total / Scores.GetLength(0));
}
static void Main(string[] args)
{
int[] Scores = new int[10];
for (int i = 0; i < Scores.Length; i++)
{
int a;
Console.Write("请输入第{0}个学生成绩 :", i);
a = int.Parse(Console.ReadLine());
Scores[i] = a;
}
int a1 = 0;
int high = 0;
for (int i = 0; i < 10; i++)
{
if (Scores[i] > high)
{
high = Scores[i];
a1 = i;
}
}
int a2 = 0;
int low =100;
for (int j = 0; j < 10; j++)
{
if (Scores[j] < low)
{
low = Scores[j];
a2 = j;
}
}
Console.WriteLine("平均成绩: {0}", Average(Scores));
Console.WriteLine("最高成绩: {0},下标是: {1}",high ,a1);
Console.WriteLine("最低成绩: {0},下标是: {1}",low ,a2);
Console.ReadLine();
}
}
}
注意考虑以下情况:
a = int.Parse(Console.ReadLine()); if (a < 0 || a > 100)
{
Console.WriteLine("输入错误");
a = a - 1;
}
else
Scores[i] = a;
a = Console.ReadLine();
a = Regex.Replace(a,@"[^\d]*", "");
使用Regex.Replace将其标点转换成字符。
在用int.Parse转换为整形数字。
int high = 0;
for (int i = 0; i < 10; i++)
{
If (Scores[i] > high)
high = Scores[i];
}
Console.Write("最高成绩为{0},下标是", high);
for (i = 0; i < 10; i++)
{
if (a[i] == high)
Console.Write(",{0}", high +1);
}
实验的运行效果如下:


实验的程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 实验三_2_
{
class Program
{
static void Main(string[] args)
{
double[] aver1 = new double[5];
double [] aver2=new double [4];
int[,] arr = new int[4, 5];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
arr[i, j] = (i + 1) * (j + 1);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
Console.Write("{0}\t", arr[i, j]);
Console.WriteLine();
}
for (int i = 0; i < 4; i++)
{
aver2[i] = 0;
for (int j = 0; j < 5; j++)
{
aver2[i] += arr[i, j];
}
aver2[i] /= 5 * 1.0;
}
for (int i = 0; i < 4; i++)
{
Console.WriteLine("第" + (i + 1) + "行的平均值为: {0}", aver2[i]);
}
Console.WriteLine();
for (int i = 0; i < 5; i++)
{
aver1[i] = 0;
for (int j = 0; j < 4; j++)
{
aver1[i] += arr[j, i];
}
aver1[i] /= 4 * 1.0;
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine("第" + (i + 1) + "列的平均值为: {0}", aver1[i]);
}
Console.ReadLine();
}
}
}
实验的运行效果如下:



总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此