<C#任务导引教程>练习一
//1,定位显示ASCI码值为30到119的字符
using System;
class Program
{
static void Main()
{
int i, n = 0;
for (i = 30; i <= 119; i++)
{
if (n % 10 == 0 && n > 0)
Console.WriteLine();
Console.Write("{0:D3}:{1} ", i, (char)i);
n++;
}
Console.WriteLine();
}
}
//2,按左下方为三角形格式输出九九乘法表
using System;
class Program
{
static void Main()
{
int m, n;
Console.Write(" *");
for (m = 1; m <= 9; m++)
Console.Write("{0,4}", m);
Console.WriteLine();
for (m = 1; m <= 9; m++)
{
Console.Write("{0,4}", m);
for (n = 1; n <= m; n++)
Console.Write("{0,4}", m * n);
Console.WriteLine();
}
}
}
//2.1,编程输出倒三角图案
using System;
class Program
{
static void Main()
{
int i, j, k;
for (i = 1; i <= 4; i++)
{
for (j = 1; j < i; j++)
Console.Write(" ");
for (k = 1; k <= 9 - 2 * i; k++)
Console.Write("*");
Console.WriteLine();
}
}
}
//3,编程输出1-100的3的倍数
using System;
class Program
{
static void Main()
{
int i, n = 0;
for (i = 1; i < 100; i++)
{
if (i % 3 != 0)
continue;
if (n % 10 == 0 && n > 0)
Console.WriteLine();
n++;
Console.Write("{0,3}", i);
}
Console.WriteLine();
}
}
//4,输入5个整数,并显示正整数,读到负整数时做出相应处理
using System;
class MainClass
{
static void Main()
{
int i;
for (i = 1; i <= 5; i++)
{
Console.Write("请输入第{0}个数:", i);
string x = Console.ReadLine();
int n = int.Parse(x);
if (n < 0)
break;
Console.WriteLine("n={0}", n);
}
Console.WriteLine("该数是负数!");
}
}
//5,输入5个整数,并显示正整数,读到负整数时做出相应处理
using System;
class MainClass
{
static void Main()
{
int i;
for (i = 1; i <= 5; i++)
{
Console.Write("请输入第{0}个数:", i);
string x = Console.ReadLine();
int n = int.Parse(x);
if (n < 0)
continue;
Console.WriteLine("n={0}", n);
}
Console.WriteLine("该数是负数!");
}
}