黑马程序员_c#基础知识学习笔记:习题ReadInt及冒泡排序

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

1.读取输入的整数,定义成方法,多次调用(如果用户输入的数字,则返回,否则提示用户重新输入)

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyCAPP
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入你的年龄?");
            int iAge = ReadInt();
            Console.WriteLine("你刚刚输入的年龄为"+iAge);

            Console.WriteLine("请输入你是哪一年出生的?");
            int iYear = ReadInt();
            Console.WriteLine("噢,你是{0}年出生的呀!",iYear);

            Console.WriteLine("请输入你们班有多少人?");
            int iCount = ReadInt();
            Console.WriteLine("你们班共有{0}人",iCount);

            Console.ReadKey();
        }
        public static int ReadInt()
        {
            int iNumber = 0;
            do
            {
                try
                {
                    iNumber = Convert.ToInt32(Console.ReadLine());
                    return iNumber;
                }
                catch
                {
                    Console.WriteLine("输入有误,请重新输入!");
                }
            }while(true);
           
        }
    }


}

运行效果为:

2.冒泡排序:

思路:让数组中的元素两两比较(第 i 个与第  i + 1 个比较),经过 n(i-1)遍两两比较,数组中的元素能按照我们预期的规律排序.

要从大到小排序,我们进行两两比较的时候用 <  符号

10 20 30 40 50 60 70      原始数据  7 个元素

20 30 40 50 60 70 10      第1趟 比较了 6 次

30 40 50 60 70 20 10      第2趟 比较了 5 次

40 50 60 70 30 20 10      第3趟 比较了 4 次

50 60 70 40 30 20 10      第4趟 比较了  3 次

60 70 50 40 30 20 10       第5趟 比较了 2 次

70 60 50 40 30 20 10       第6趟 比较了 1 次

也就是 n 个数需要排 n - 1 趟

第 t  趟比较的次数为: n -  t 次

循环变量 i = 0 第一趟

                  i = 1 第二趟

由此可知趟数: i + 1;  即 t = i + 1;

for(int j = 0;j<n-t;j++) 因为 t = i+1 所以此循环条件可以改成 j < n - i - 1;

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyCAPP
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] scores = { 18,20,48,76,20,38,87,90,37,45,65,65,34,97};

            for (int i = 0; i < scores.Length - 1; i++)  //控制比较的趟数
            {
                for (int j = 0; j < scores.Length -i- 1; j++)
                {
                    if(scores[j] < scores[j+1])
                    {
                        int temp = scores[j];
                        scores[j] = scores[j+1];
                        scores[j + 1] = temp;
                    }
                }
            }

            for (int i = 0; i < scores.Length; i++)
            {
                Console.Write(scores[i]+ " ");
            }

            Console.ReadKey();

        }
      
    }

}

运行代码及效果为:

 

 

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net/heima/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值