Home > other >  C # class method
C # class method

Time:09-19

Title description

In class Class1, write a class methods IsEven (string number) for the parity of the output parameters, and is verified in the Main Program class output,

Class Program
{
The static void Main (string [] args)
{
Console. Write (" Input Integer: ");
String number=Console. ReadLine ();
Console. WriteLine (Class1. IsEven (number));
}
}

Enter

Enter an integer

O

Output parity sex

The sample input



Sample output



Tip

Invalid input integer how to deal with? Such as 18 k, 17.9

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

Using System. The Threading. The Tasks;

The namespace ConsoleApplication1

{

The class Class1

{

//internal: the same assembly object can access

Internal static string IsEven (string number)

{

int n;

//1. Determine whether the input effective

Bool x=int. TryParse (number, out n);

//2. Invalid

If (x==false)

{

Return "invalid integer";

}

//3. Even

If (n % 2==0)

{

Return "even";

}

An odd
//4.
The else

{

Return "odd";

}

}

}

Class Program

{

The static void Main (string [] args)

{

//the Console. Write (" Input Integer: ");

String number=Console. ReadLine ();

Output
//5.
Console. WriteLine (Class1. IsEven (number));

}

}

}




  • Related