【C#入门练习及答案】

循环

循环结构(一)

1.为了帮助张浩尽快提高成绩,老师给他安排了每天的学习任务,其中上午阅读教材,学习理论部分,下午上机编程,掌握代码部分。老师每天检查学习成果。如果不合格,则继续进行.

Console.WriteLine("合格了吗?(y/n)");
char answer = char.Parse(Console.ReadLine());
while (answer=='n')
{
    
    
      Console.WriteLine("上午阅读");
      Console.WriteLine("下午编程");
      Console.WriteLine("及格了吗?(y/n)");
      answer =char.Parse(Console.ReadLine());
}
Console.WriteLine("完成学习任务!");
  1. 2006年培养学员8万人,每年增长25%,请问按此增长速度,到哪一年培训学员人数将达到20万人?
int year = 2006;
double student = 80000;
while (student < 200000)
{
    
    
    student = student + (student * 0.25);
    year++;
}
Console.WriteLine("在第{0}年达到20万人", year);
  1. 经过几天的学习,老师给张浩一道测试题,让他先上机编写程序完成,然后老师检查是否合格。如果不合格,则继续编写。
char answer;
do{
    
    
     Console.WriteLine("上机编程");
     Console.WriteLine("合格了吗?");
     answer = char.Parse(Console.ReadLine());
} while (answer=='n');
Console.WriteLine("恭喜你通过了测试!");

4.使用do-while实现:输出摄氏温度与华氏温度的对照表,要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的条目不超过10条。
转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 32
提示:
1、循环操作:计算摄氏温度,并输出对照条目
2、循环条件:条目<=10 && 摄氏温度 <= 250

double sheshi = 0;
int tiaomu = 0;
do
{
    
    
     double huashi = sheshi * 9 / 5.0 + 32;
     Console.WriteLine("摄氏温度 {0}   华氏温度{1}", sheshi,huashi);
     sheshi += 20;
     tiaomu++;
} while (tiaomu < 10 && sheshi <= 250);

循环结构(二)

1.实现整数反转

            Console.WriteLine("请输入一个整数:");
            int i = int.Parse(Console.ReadLine());
            Console.WriteLine("反转之后得:");
            while (i!=0)
            {
    
    
                int b = i % 10;     //求余
                Console.Write(b);
                i = i /10;
            }


2.循环输入某同学S1结业考试的5门课成绩,并计算平均分

for (int i = 0 ; i < 5 ; i++) {
    
      	//循环5次录入5门课成绩
Console.Write("请输入5门功课中第{0}门课的成绩: ", i +1 );
score = int.Parse(Console.ReadLine());  	//录入成绩
sum = sum + score;        	//计算成绩和
}
avg = (double)sum / 5;                      //计算平均分
Console.WriteLine(  "的平均分是:{0}" , avg);

3.输出如图所示加法表
在这里插入图片描述

for(  int i = 0,  j = val;  i<=val;  i++,  j-- ){
    
    
        Console.WriteLine("{0} + {1} = {2}" , i , j , i+j );
 }

4.求1~100之间不能被3整除的数之和

            Console.WriteLine("1~100不能被3整除的数之和为:");
            int sum = 0;
            for(int i=0; i < 100; i++)
            {
    
    
                if (i % 3 != 0)
                {
    
    
                    sum += i;
                }
                
            }
            Console.WriteLine(sum);

5.循环录入某学生5门课的成绩并计算平均分,如果某分数录入为负,停止录入并提示录入错误

  			int score;
            int sum=0;
            for (int i = 0; i < 5; i++)
            {
    
           //循环5次录入5门课成绩  
                Console.Write("请输入第" + (i + 1) + "门课的成绩: ");
                score = int.Parse(Console.ReadLine());
                if (score < 0)
                {
    
          //输入负数
                    bool isNegative = true;
                    Console.WriteLine("录入错误");
                    break;
                }
                sum += score;         //累加求和
                }

6.1~10之间的整数相加,得到累加值大于20的当前数

            int sum = 0;//存储总和
            for (int i = 1; i <= 10; i++)
            {
    
    
                sum += i;
                if (sum >= 20)//总和大于等于20
                {
    
    
                    Console.WriteLine("加到{0}的时候,总和大于了20", i);
                    break;//条件成立跳出循坏
                }
            }

7.循环录入C#课的学生成绩,统计分数大于等于80分的学生比例

		    int total=10;
            int score;
            int num=0;
            for (int i = 0; i < total; i++)
            {
    
    
                Console.Write("请输入第{0}位学生的成绩:", i + 1);
                score = int.Parse(Console.ReadLine());
                if (score < 80)
                {
    
    
                    continue;//跳出本次循环,执行下一次循环
                }
                num++;
            }
            Console.WriteLine("80分以上的学生人数是: {0}", num);
            double rate = (double)num / total * 100;
            Console.WriteLine("80分以上的学生所占的比例为:{0}%", rate);

循环结构(进阶)

  • 模拟商场购物
  • 输出九九乘法表

1.数组中的英文歌曲按照名称升序排列。增加一首新歌,并保持歌曲名称升序排列

	String[ ] musics = new String[]{
    
    "Island","Ocean","Pretty","Sun"};
	String[ ] newMusics = new String[musics.length+1];//新歌曲数组
	String music = "";	//保存用户输入的歌曲名称
	……
	for(int i = 0; i < musics.length; i++){
    
    
		if(musics[i].CompareTo (music) > 0){
    
    
			index = i;
			break;
		}
	}
	for(int i = newMusics.length-1; i > index; i--){
    
    
		newMusics[i] = newMusics[i-1];  
	}
	newMusics[index] = music;    
	……

2.3个班级各4名学员参赛,计算每个班级参赛学员的平均分

ps:用外层循环控制班级数目,内层循环控制每个班级学员数目

			int classNum=3;
			double sum;
			double[] score = new double[3];
			double[] aver = new double[3];
			for (int i = 0; i< classNum; i++)
			{
    
    
				sum = 0.0;
				Console.WriteLine("请输入第{0}个班级的成绩", i + 1);
				for (int j = 0; j < score.Length; j++)
				{
    
    
					Console.Write("第{0}个学员的成绩:", j + 1);
					score[j] = int.Parse(Console.ReadLine());
					sum = sum + score[j];
				}
				aver[i] = sum / score.Length;           //计算平均分
				Console.WriteLine("第{0}个班级平均分{1}\n",i+1, aver[i]);
			}

3.3个班级各4名学员参赛,计算每个班级参赛学员平均分,统计成绩大于85分学员数
要求:使用continue统计>85的学员人数

			int classNum=3;
			double sum;
			double[] score = new double[3];
			double[] aver = new double[3];
			int count=0;
			for (int i = 0; i < classNum; i++)
			{
    
    
				sum = 0.0;
				Console.WriteLine("请输入第{0}个班级的成绩", i + 1);
				for (int j = 0; j < score.Length; j++)
				{
    
    
					Console.Write("第{0}个学员的成绩:", j + 1);
					score[j] = int.Parse(Console.ReadLine());
					sum = sum + score[j];
					if (score[j] < 85)
					{
    
    
						continue;
					}
					count++;

				}
				aver[i] = sum / score.Length;           //计算平均分
				Console.WriteLine("第{0}个班级平均分{1}\n",i+1, aver[i]);
				Console.WriteLine("高于85分的学员有{0}个\n",count);

4.用*打印直角三角形图案

		int rows = 3;					 //三角形行数
		Console.WriteLine("打印直角三角形");
		for(int i = 0; i < rows; i++){
    
        //打印第i行
			for(int j = 0; j <= i; j++){
    
      //打印i个*号
				Console.Write("*");
			}
			Console.Write("\n");	 //换行
		}

5.有5家衣服专卖店,每家最多购买3件。用户可以选择离开,可以买衣服。最后打印总共买了几件衣服
提示:二重循环解决 外层循环控制去每个专卖店 内层循环控制买衣服过程 使用break退出内层循环

			char choice;
			int count = 0;
			for (int i = 0; i < 5; i++)
			{
    
    
				Console.WriteLine("欢迎光临第{0}家专卖" , i + 1);
				for (int j = 0; j < 3; j++)
				{
    
    
					Console.WriteLine("要离开吗(y/n)?");
					choice = char.Parse(Console.ReadLine());
					if ('y'.Equals(choice))
					{
    
    
						break;
					}
					Console.WriteLine("买了一件衣服");
					count++;    //计数器加1
				}

6.实现九九乘法表

		int rows = 9;	//乘法表的行数
		for(int i = 1; i<=rows; i++){
    
    	//一共9行
			for(int j = 1; j <= i; j++){
    
    	
				Console.Write(j+"*"+i+"="+j*i+"	");    
			}
			Console.Write("\n");			
		}
	}

数组

1.计算全班学员的平均分

			int[] scores = new int[5];  //成绩数组
			int sum = 0;            //成绩总和

			Console.WriteLine("请输入5位学员的成绩:");
			for (int i = 0; i < scores.Length; i++)
			{
    
    
				scores[i] = int.Parse(Console.ReadLine());
				sum = sum + scores[i];  //成绩累加
			}
			Console.WriteLine("平均分是{0}", (double)sum / scores.Length);

2.循环录入5位学员成绩,进行升序排列后输出结果

			int[] a = new int[5];
			int t;
			Console.WriteLine("请输入五位学生的成绩:");
			for (int i = 0; i < a.Length; i++)
			{
    
    
				a[i] = int.Parse(Console.ReadLine());//循环录入
			}


			for (int j = 0; j < a.Length - 1; j++)//冒泡排序
			{
    
    
				for (int k = 0; k < a.Length - j - 1; k++)
				{
    
    
					if (a[k] > a[k + 1])
					{
    
    
						t = a[k + 1];
						a[k + 1] = a[k];
						a[k] = t;
					}
				}
			}

			Console.Write("学员成绩升序排列 : ");
			for (int i = 0; i < a.Length; i++)
			{
    
    
				Console.Write("{0} ", a[i]);
			}

3.从键盘输入本次Java考试五位学生的成绩,求考试成绩最高分

			int[] a = new int[5];
			int i,max;
			max = a[0];
			Console.WriteLine("请输入五位学生的成绩:");
			for ( i = 0; i < a.Length; i++)
			{
    
    
				a[i] = int.Parse(Console.ReadLine());//循环录入
			}

			for (i = 1; i < a.Length; i++)//打擂台法比较
			{
    
    
				
                if (max < a[i])
                {
    
    
					max = a[i];
                }
			}

			Console.Write("学员成绩最高分 :{0}",max);

类和对象

1.在不同培训中心,会感受到相同的环境和教学氛围,用类的思想输出中心信息
在这里插入图片描述

class School
		{
    
    
			public string schoolName;       //中心名称
			public int classNumber;            //教室数目
			public int labNumber;           //机房数目

			//定义教室的方法
			public void showCenter()
			{
    
    
				Console.WriteLine("{0}培训学员\n配备:{1}教室数目{2}机房数目",schoolName, classNumber, labNumber);
			}
		}

2.创建“北京中心”对象

class InitialSchool {
    
    
	static void Main(string[] args)
        {
    
    

			
				School center = new School();
				Console.WriteLine("***初始化成员变量前***");
				center.showCenter();
				center.schoolName = "北京中心";
				center.classNumber = 10;
				center.labNumber = 10;
				Console.WriteLine("\n***初始化成员变量后***");
				center.showCenter();
			
		}
}

3.编写学生类,输出学生相关信息;在这里插入图片描述

public class Student {
    
    
	String name;	//姓名
	int age;			//年龄
	String classNo;	//班级
	String hobby;	//爱好
	//输出信息方法
	public void show(){
    
    
		Console.WriteLine({
    
    0}\n年龄:{
    
    1}\n就读于:{
    
    2}\n爱好:{
    
    3}" , name, age, classNo ,hobby);
	}
}
class InitialStudent {
    
    
	 static void Main(String [] args){
    
    
		Student student = new Student();	
		student.name = "张浩";		
		student.age = 10;
		student.classNo = "S1班";
		student.hobby = "篮球";
		student.show();		               	
	}
}

4.一个景区根据游人的年龄收取不同价格的门票。请编写游人类,根据年龄段决定能够购买的门票价格并输出
在这里插入图片描述

class Visitor {
    
    
	public string name;		//姓名
    public int age;			//年龄
	//显示信息方法
	public void show(){
    
    
			if(age>=18 && age<=60){
    
    		//判断年龄
				Console.WriteLine({
    
    0}年龄为{
    
    1},价格为20元" ,name,age);
			}else{
    
    
				Console.WriteLine({
    
    0}的年龄为:{
    
    1},免费“,name,age);				}
	}
}
class InitialVistor {
    
    
	static void main(string[] args) {
    
    		
		Visitor v = new Visitor();		
		Console.Write("请输入姓名:");
		v.name = Console.ReadLine();			
		Console.Write("请输入年龄:");
		v.age = int.Parse(Console.ReadLine());		
		v.show();                       
	}
}

5.修改学生姓名,输入新、旧姓名,进行修改并显示是否修改成功

  public class StudentsBiz {
    
    
	  string[ ] names = new string[30];public bool editName (string oldName,string newName) {
    
    
		  bool find = false;  // 是否找到并修改成功标识		
		  // 循环数组,找到姓名为oldName的元素,修改为newName
		  for(int i=0;i<names.length;i++){
    
    
			  if(names[i].Equals(oldName)){
    
    
				  names[i] = newName;
				  find=true;
				  break;
			  }
		  }
		  return find;
	  }
	}
  public class TestModify {
    
    
	  public static void Main(string[] args) {
    
    
	      ……
		  Console.Write("\n请输入要修改的学生姓名:");
		  string oldname = Console.ReadLine();
		  Console.Write("\n请输入新的学生姓名:");
		  string newname = Console.ReadLine();
		  Console.WriteLine("\n*****修改结果*****");
		  if( st.editName(oldname, newname) ){
    
    
			  Console.WriteLine("找到并修改成功!");
		  }else{
    
    
			  Console.WriteLine("没找到该学生!");
		  }
		  st.showNames();	
	  }
}

6.指定查找区间,查找学生姓名并显示是否修改成功

public bool searchName (int start,int end,string name){
    
    
	bool find = false;  // 是否找到标识
	// 指定区间数组中,查找姓名
	for(int i=start-1;i<end;i++){
    
    
	      if(names[i].Equals(name)){
    
    	
		  find=true;
		  break;
	      }
	}
	return find;
}
if(st.searchName(s,e,name)){
    
    
	Console.WriteLine("找到了!");
}else{
    
    
	Console.WriteLine("没找到该学生!");
}

猜你喜欢

转载自blog.csdn.net/QwwwQxx/article/details/129476317