A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马王刚 黑马帝   /  2012-5-1 10:13  /  1750 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

           Console.WriteLine("请输入一个年份!");
            int year = Convert.ToInt32(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;
            }
        }
运行报错,提示错误   “返回值.Program.ReadAnswer()”: 并非所有的代码路径都返回值

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

4 个回复

倒序浏览
public static bool LeapYear(int year)
{
if (year%400 == 0 || year%4==0 && year%100!=0)
{
return true;
}
else
{
return false;
}
}

改成这样
  1. public static bool LeapYear(int year)
  2. {
  3. bool b = false;
  4. if (year%400 == 0 || year%4==0 && year%100!=0)
  5. {
  6. b=true;
  7. }
  8. else
  9. {
  10. b=false;
  11. }
  12. return b;
  13. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             Console.WriteLine("请输入一个年份!");
            int year = Convert.ToInt32(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;
            }
        }
        }
    }
没错的啊

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
Program.ReadAnswer()”:怎么没看见这方法呢

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
program.ReadAnswer()这个方法声明时要有返回类型,并且要有一个方法体中要有一个return语句;return回一个和返回类型一样类型的值;
你代码报错的提示的意思是要么没有声明返回类型,要么没有return语句;

评分

参与人数 1技术分 +2 收起 理由
宋天琪 + 2

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马