仿C版本
using static System.Console;
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
int[] shu = { 18, 2, 15, 23, 65, 47 };
ShowArray(shu);
WriteLine($"Sum = {Sum(shu)}");
WriteLine();
int x;
while (GetInteger(out x))
{
WriteLine(Contain(shu, x) ? "Yes" : "No");
}
}
private static void ShowArray(int[] arr)
{
foreach (int item in arr)
{
Write($"{item} ");
}
WriteLine();
}
private static int Sum(int[] arr)
{
int sum = 0;
foreach (int item in arr)
{
sum += item;
}
return sum;
}
private static bool Contain(int[] arr, int x)
{
bool found = false;
foreach (int item in arr)
{
if (item == x)
{
found = true;
break;
}
}
return found;
}
private static bool GetInteger(out int x)
{
bool success = false;
x = 0;
bool done = false;
while (!done)
{
Write("Input a integer: ");
string s = ReadLine();
if (!string.IsNullOrEmpty(s))
{
done = int.TryParse(s, out x);
success = done;
}
else
{
done = true;
}
}
return success;
}
}
}
[此贴子已经被作者于2019-5-1 00:47编辑过]