课后作业
1.说说你学过哪些循环
for 循环 while循环 do while循环 双重for循环foreach循环
2.编写C#程序,定义一个数组 int [i] number = new int []{1,2,3,4,5};使用fo’reach循环输出数组中的数据,要求遇到3将不输出,遇到5退出循环
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @foreach
{
class Program
{
static void Main(string[] args)
{
int[] number = new int[] { 1, 2, 3, 4, 5};
foreach (int i in number)
{
if (i == 3) {
continue;
}
if (i == 5)
{
break;
}
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
3.编写程序实现1 22 333 4444 55555的效果
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ForXunHuan
{
class Program
{
static void Main(string[] args)
{
int i, j; //循环变量
//外层循环控制每行打印数字的个数
for (i = 1; i <= 5; i++) {
//内层循环控制每行打印数字的个数
for (j = 1; j <=i; j++) {
Console.Write(i); //打印一个数字
}
Console.WriteLine(); //打印一行数字换行
}
Console.ReadLine();
}
}
}
4.编写代码实现接受一个班级五名学员的姓名和三门课程的考试成绩,计算每名学员三门课程的总成绩和平均成绩,并输出学员的姓名,三门课程的考试成绩,总成绩和平均成绩,以及班级参加考试人数,三门课总和的最高分和平均分
Student类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 课后作业
{
class Student
{
double sum = 0;
double avg = 0;
public void students()
{
Console.Write("学员姓名:");
string student = Console.ReadLine();
}
public double getScore()
{
double[] score = new double[3];
for (int i = 0; i < score.Length; i++) {
Console.Write("第{0}门课程考试成绩是:",i+1);
score[i] = double.Parse((Console.ReadLine()));
sum = score[0] + score[1] + score[2];
}
return sum;
}
public void show()
{
double[] student = new double[5];
for (int i = 0; i < student.Length; i++)
{
students();
double sums = getScore();
Console.Write("三门课程考试总成绩是:" + sums);
double avg = sums / 3;
Console.Write("平均成绩是:" + avg);
student[i] = sums;
Console.WriteLine("\n");
}
int temp = 0;
foreach (int item in student)
{
if (item > temp)
{
temp = item;
}
}
double allavg, allsum = 0;
foreach (double item in student)
{
allsum += item;
}
allavg = allsum / student.Length;
Console.WriteLine("班级参考人数:{0},最高分:{1}, 总分平均成绩{2}", student.Length, temp, allavg);
Console.ReadLine();
}
}
}
主方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 课后作业
{
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.show();
}
}
}
5.编写程序,输入电脑配件的价格,从高到低排序输出,同时计算这台电脑的最低/最高总价格
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CPU
{
class Program
{
static void Main(string[] args)
{
//CPU GPU RAM SSD CD PLAN DISPLAY 处理器 显卡 内存条 固态硬盘 光驱 主机 显示器
int [] computer = new int[7]; //电脑价格数组
int i, j; //循环变量
int temp; //临时变量
int Max = 0; //最大值
int Min = 0; //最小值
int Sum; //价格总和
Console.WriteLine("请依此输入电脑配件的价格:");
for (i=0; i<computer.Length; i++ ) {
Console.WriteLine("请输入{0}个电脑配件的价格:",i+1);
computer[i] = int.Parse((Console.ReadLine())); //类型转换
}
//排序
for (i = 0; i<computer.Length-1; i++) {
for (j = 0; j<computer.Length-1-i; j++) {
if (computer[j]<computer[j+1]) {
//交换元素
temp = computer[j];
computer[j] = computer[j + 1];
computer[j + 1] = temp;
}
}
}
Console.WriteLine("排序输出:");
for (i = 0; i<computer.Length; i++) {
Console.Write("{0}\t",computer[i]);
}
Max = computer.Max();
Min = computer.Min();
Console.Write("\n最高价为:{0},最低价为{1}",Max,Min);
Sum = computer[0] + computer[1] + computer[2] + computer[3] + computer[4] + computer[5] + computer[6];
Console.WriteLine("\n这台电脑总价为:{0}",Sum);
Console.ReadLine();
}
}
}