首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c#练手code

c#练手code

作者头像
热心的社会主义接班人
发布2018-05-16 15:56:20
5890
发布2018-05-16 15:56:20
举报
文章被收录于专栏:cscscs

做一个控制台程序,要求输入三个任意整数,将三个数按从大到小的顺序输出。

根据排列组合,知道有6中情况,一是可以采用排序,不过有些小题大做。二是可以交换,输出。

方法一

采用交换,让a最大,b第二大,就行了。

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

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" put into  3 numbers");
            int a, b, c;
            a = int.Parse(Console.ReadLine());
            b = int.Parse(Console.ReadLine());
            c = int.Parse(Console.ReadLine());
            display(a, b, c);
        }

        static void  display(int a, int b, int c)
        {
    //保证a最大
        if(a<b) 
         swap(ref a, ref b); 
        if(a<c)
        swap(ref a, ref c);
    //保证b第二大
        if(b<c)
        swap(ref b, ref c);
           Console.WriteLine("{0},{1},{2}", a, b, c);
        }

    // 交换函数
        static void swap(ref int a,ref int b)
        {
           int temp = a;
            a = b;
            b = temp;
        }    

    }
}
 put into  3 numbers
55
88
111
111,88,55
请按任意键继续. . .

采用冒泡排序,搞定

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

namespace homework1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" put into  3 numbers");
            int a,b,c;
            a = int.Parse(Console.ReadLine());
            b = int.Parse(Console.ReadLine());
            c = int.Parse(Console.ReadLine());

            Console.WriteLine("the array:");
            int[] num = { a, b, c };
            display(num);
            sort(num);
            Console.WriteLine(" sort the end");
            display(num);


        }

        static void sort(int[] str)
        {
            for(int i=0; i<str.Length-1; i++)
                for (int j = 0; j < str.Length - i - 1; j++)
                {
                    if (str[j] < str[j + 1])
                    {
                        int temp = str[j];
                        str[j] = str[j + 1];
                        str[j + 1] = temp;
                    }

                }

        }



        static void display(int[] str)
        {

            foreach (int n in str)
                Console.WriteLine(n);
        }




    }
}
 put into  3 numbers
11
22
66
the array:
11
22
66
 sort the end
66
22
11
请按任意键继续. . .

定义一个student的结构体,有姓名,分数属性,输入3组数据,在输出,并求出平均分

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

namespace homework3
{
    class Program
    {

        struct Student
        {
            public string name; //姓名
            public float math; //数学成绩
        }

        static void Main(string[] args)
        {

            Student[] s = new Student[4];
            s= input(s);
            display(s);
            //  double ave = s[0].math + s[1].math + s[2].math;
            float ave = s[s.Length - 1].math;
            Console.WriteLine("the average mark is :" + ave/3);
            Console.WriteLine();

        }

        static Student[] input(Student[] s)
        {
            float sum = 0;
            for(int i=0; i<s.Length-1; i++)
            {
                
                Console.WriteLine("put "+(i+1)+" student name and mark:");
                s[i].name = Console.ReadLine();
                s[i].math = (float)Convert.ToDouble(Console.ReadLine());
                sum += s[i].math;
            }
            s[s.Length - 1].math = sum;
            return s;
        }

        static void display(Student[] s)
        {
            for(int i=0; i<s.Length; i++)
            {
                Console.WriteLine("{0}的数学成绩是:{1}",s[i].name,s[i].math);
            }
        }

    }
}

结果是:

put 1 student name and mark:
a
85
put 2 student name and mark:
b
90.5
put 3 student name and mark:
c
76.5
a的数学成绩是:85
b的数学成绩是:90.5
c的数学成绩是:76.5
的数学成绩是:252
the average mark is :84
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.05.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档