| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 694 人关注过本帖
标题:[求助]为什么这里要有return
只看楼主 加入收藏
hanxuema
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-7-28
收藏
 问题点数:0 回复次数:4 
[求助]为什么这里要有return

using System;

class Class1
{
public static void Main()
{
string command;

while ((command = Console.ReadLine()) != "exit")
{
switch (command)
{
case "get":
doget();
break;

case "put":
doput();
break;

case "default":
doDefault();
break;
}
}
}

public static int doget()
{
Console.WriteLine("get is ok");
return 0;
}

public static int doput()
{
Console.WriteLine("put is ok");
return 0;
}
private static int doDefault()
{
Console.WriteLine("the command is wrong");
Console.WriteLine("whileApp V0.1 supprts:");
Console.WriteLine("\tget:\t");
Console.WriteLine("\tput:\t");
Console.WriteLine("\texit:\t");

return 0;
}
}



关于这个程序,请问在 public static int doget()
{
Console.WriteLine("get is ok");
return 0;
}中为什么一定要return 0; 我不写return好像不行,请问是为什么,错误报告说需要一个int,谁能详细解释一下,因为为什么要public static int doget 里面的这个int 我都不知道

或者return 100可以吗?

谢谢大家

搜索更多相关主题的帖子: return 
2006-08-11 03:03
从小到大
Rank: 1
等 级:新手上路
威 望:2
帖 子:200
专家分:0
注 册:2006-6-24
收藏
得分:0 
public static int doget
这个必须要有INT返回值 因此要return 0;100也可以

[此贴子已经被作者于2006-8-11 8:32:51编辑过]


2006-08-11 08:31
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 

因為你自己把這個方法定義為 public static int doget() ,所以要返回個int變量

public static void doget() 定義為void的就不強制需要了


[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-11 08:39
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 

其实这个是跟国际接轨后的要求
在ISO C89中MAIN函数都不要返回的
但是到了ISO C99协议中 函授都要返回了

感觉这个要求可以理解有利于程序的结构化 规范化

此号自封于2006年11月30日
2006-08-11 10:00
月夜枫华
Rank: 4
等 级:贵宾
威 望:12
帖 子:437
专家分:42
注 册:2006-1-2
收藏
得分:0 

你定义的就是一个返回整型数值的函数,不返回值你写它干什么.


2006-08-11 17:43
快速回复:[求助]为什么这里要有return
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019722 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved