c# - 如何获得用户的输入并提供反馈
问题描述
我是编码的新手 - 刚刚开始。构建成功,但我想添加输入输出以获得消费者响应,以检查它是否真的有效。谢谢
using System;
namespace LeapYear
{
public static class Leap
{
static void Main(string[] args)
{
Console.WriteLine("Enter a year ");
Console.ReadLine();
}
public static bool IsLeapYear(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 ==0)
{
return true;
}
return false;
}
}
}
解决方案
干得好:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Enter a year ");
//Console.ReadLine returns the entered data as a string
var yearString = Console.ReadLine();
//Convert the string to an int
int year = Int32.Parse(yearString);
//Now we can call your function, passing the year variable and recording the bool value passed back
var isLeapYearAnswer = IsLeapYear(year);
//Print the answer to the console
Console.WriteLine(isLeapYearAnswer);
}
public static bool IsLeapYear(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 ==0)
{
return true;
}
else
{
return false;
}
}
}
这是一个dotnetfiddle
推荐阅读
- r - 使用 dplyr,计算每个班级的非数字成绩
- mysql - MySQL 组连接表
- java - JPQL 意外 AST 节点:围绕“合并”
- r - 在 R Shiny 中的 observeEvent 函数中单击单元格时出现问题
- sql - postgres 中如何处理空值/数组?
- python - 如何在我的 django 模板中的选择标签上显示外键?
- php - InlineButton 应该更改数据库中的值(日期)
- java - 使用 Files.createSymbolicLink() 方法创建符号链接时抛出 NoSuchFileException
- java - ID生成方法陷入循环
- function - Godot3 如何调用在另一个场景中创建的对象的方法