I am new and I am learning to use methods and such and I have my code in the method but in that code there is a Console.ReadLine(); which I don't want. Basically what I need to do is a big assignment with 10 methods, as input you have to call which method and then the actual input, but when there is a readline in my method I have to give 3 things instead of 2. My code is below if anyone know how to help a noob ?
Basically the int.parse(consolereadline); I need to somehow remove it so I can only read in the Method number and then actual input.
static object GetNextLeapYear(int year1)
{
int n, year;
n = int.Parse(Console.ReadLine());
year = n 4 - (n % 4);
if ((year % 100 == 0) && (year % 400 != 0))
{
year = 4;
}
return year;
}
static void Main(string[] args)
{
string method = Console.ReadLine();
switch (method)
{
case "1":
int input = Convert.ToInt32(Console.ReadLine());
GetNextLeapYear(input);
Console.WriteLine($"{GetNextLeapYear(input)}");
break;
CodePudding user response:
I think that what you're looking for is to remove the first call to GetNextLeapYear(input);
in the case "1"
body. You're not storing the result of that call and you're actually displaying the result of the second call that is in the Console.WriteLine($"{GetNextLeapYear(input)}");
which causes an additional (and unnecessary) call to Console.ReadLine in the GetNextLeapYear method.
CodePudding user response:
just remove "n = int.Parse(Console.ReadLine())" I don' t why do you need it at all
static int GetNextLeapYear(int inputYear)
{
var year = inputYera 4 - (inputYear % 4);
if ((year % 100 == 0) && (year % 400 != 0)) year = 4;
return year;
}
and code
Console.WriteLine("pls, type a year");
int input = Convert.ToInt32(Console.ReadLine());
var leapYear = GetNextLeapYear(input);
Console.WriteLine($"next leap year is {leapyear}");
but you would use something like this as a method, if it is not your high school project
public static int GetNextLeapYear(int year)
{
do {
year=year 1;
if (DateTime.IsLeapYear(year)) return year;
} while (true)
}
and added input validation in main
int year=0;
bool success = false;
do {
Console.WriteLine("pls, type a correct year number");
var input = Console.ReadLine();
success=int.TryParse(value, out year);
while (success);
var leapYear = GetNextLeapYear(year);
Console.WriteLine($"next leap year is {leapyear}");