c# - 如何从 if 语句中返回值?
问题描述
我不仅对 C# 而且对整个编程都很陌生,我知道这是一个非常基本的问题,但我只是不知道如何从 if 语句中返回一个值。这是我的代码:
static void Main(string[] args)
{
int couscous = RandomNumber(1);
Random rnd = new Random();
while (couscous > 1)
{
int remainder = couscous % 3;
if (remainder == 0)
{
Console.WriteLine(couscous / 3);
int step2 = couscous / 3;
// I want to return couscous / 3 to the beginning of the while statement so it repeats all the code but with step2 instead of couscous.
}
else if (remainder != 0)
{
Console.WriteLine(couscous + 1);
}
}
}
static int RandomNumber(int i)
{
Random rnd = new Random();
i = rnd.Next(1, 1000);
int input = i;
return input;
解决方案
你的意思是这个,你也会了解更多关于return的信息。
因为 void 不能有回报。
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(ifReturn());
}
static string ifReturn()
{
bool good = true;
if (good)
{
return "It was true";
}
else
{
return "It was false";
}
}
}
}
推荐阅读
- python - 在 Python 环境中自动运行代码生成工具(例如 thrift)的最佳实践
- asp.net-core - 将视频流重定向到视频播放器(如 HTML5 视频播放器)的最佳方法
- redirect - 将我的博客设为主页?重定向?
- javascript - 在节点 js 中对 json 文件使用 GET 时遇到问题
- maven - Teamcity - 如何在不使用工件存储库的情况下解决其他模块的 Maven 依赖项?
- php - (HY000/1045) 用户 'root'@'localhost' 的访问被拒绝(使用密码:是)
- sql - 如何将 SQL Server RDS 备份导入 SQL Server Linux Docker 实例?
- python - 线性回归 python
- php - 如何在 WordPress 中划分网站的不同部分?
- javascript - 无法呈现对 html div 的承诺响应