• 返回值


    返回值 

    1)当调用者想访问我们方法中 的变量时可以通过返回值返回

    例 如

    string s=Console.ReadLine();

    int i= Covert.ToInt32("22")

    2)为什么方法前面能够定义一个变量接收到方法的值 是因为在方法中使用了返回值。

    3)只要在方法中返回了值,那么在调用方法中,前面就应该用一个变量来接收方法的返回值.

    4)一个方法只能有一个返回值

    5)一但一个方法有返回值,那么在这个方法体中就必须通过return语句返回一个值,并且这个值要与返回值类型相同

    语法:return 值;


    static void Main(string[] args)
    {
    Console.WriteLine("你确定要关机吗(y/n)");
    string s=ReadAnswer();
    //在Main方法中,我能知道用户输入提y还是n吗?
    if(s=="y")
    {
    Console.WriteLine("正在关机");
    }
    else
    {
    Console.WriteLine("不关机");

    }
    Console.ReadKey();
    }

    public static string ReadAnswer()
    {
    string result = "";
    do
    {
    result=Console.ReadLine();
    if (result != "y" && result != "n")
    {
    Console.WriteLine("输入有误 请重新输入");
    }

    }while(result!="y"&&result!="n");
    //当方法执行完成后.result中的就是用户输入的Y N
    return result;


    static void Main(string[] args)
    {


    int a = int.Parse(Console.ReadLine());
    int b = int.Parse(Console.ReadLine());
    int sum = Add(a, b);
    Console.WriteLine("平均值 为{0}",sum/2);
    Console.ReadKey();
    }




    //求2个整数和
    public static int Add(int a,int b)
    {

    return a + b;
    }


    static void Main(string[] args)
    {
    int year = int.Parse(Console.ReadLine());
    bool result = LeapYear(year);
    if (result)
    {

    Console.WriteLine("闰年");
    }
    else
    {
    Console.WriteLine("不是闰年");

    }

    Console.ReadKey();
    }

    public static bool LeapYear(int year)
    {
    if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
    {
    return true;


    }
    else
    {
    return false;

    }

    }


    namespace 写一个方法求最大值
    {
    class Program
    {
    static void Main(string[] args)
    {
    int max = Max(10,20);
    Console.WriteLine(max);
    Console.ReadKey();

    }

    public static int Max(int i1, int i2)
    {
    if (i1 > i2)
    {
    return i1;

    }
    else
    {
    return i2;

    }


    static void Main(string[] args)
    {
    //int max = Max(10,20);
    //Console.WriteLine(max);
    int[] n = { 1, 2, 3, 4, 5, 6 };
    int result=Sum(n);
    Console.WriteLine(result);
    Console.ReadKey();

    }
    public static int Sum(int[] numbers)
    {
    int numbersSum = 0;
    for (int i = 0; i < numbers.Length;i++ )
    {
    numbersSum += numbers[i];


    }
    return numbersSum;

  • 相关阅读:
    day3
    day2
    day1-存储
    day5-iptables
    MySQL之补充
    11.18
    11.17
    junit基础学习之-测试controller层(2)
    junit基础学习之-简介(1)
    外键和级联
  • 原文地址:https://www.cnblogs.com/swlq/p/5378314.html
Copyright © 2020-2023  润新知