zoukankan      html  css  js  c++  java
  • 返回值

    返回值 

    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;

  • 相关阅读:
    MVP模式
    开源代码SlidingMenu的使用
    常用命令(Linux、Android、adb)
    一文搞清楚Minor GC、Major GC 、Full GC 之间的关系
    阿里最新38道Java面试题解析(MyBatis+消息队列+Redis)
    从5个方面让你真正了解Java内存模型
    深入理解JVM:元空间大小详细解析
    面试必问:JVM类加载机制详细解析
    5个点彻底搞清楚SpringBoot注解
    8种创建Java线程的方式,你知道几个?
  • 原文地址:https://www.cnblogs.com/swlq/p/5378314.html
Copyright © 2011-2022 走看看