1.打印*****
****
***
**
*(for循环)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ketang
{
class Program
{
static void Main(string[] args)
{
int a;
int b; // 定义两个变量
for (a = 1; a <= 5; a++) //一个变量控制层数
{
for (b = 5; b >= a; b--) //一个变量控制输出*的个数
{
Console.Write("*"); //打印*
}
Console.WriteLine(); //换行
}
}
}
}
2.打印*
**
***
****
*****(for循环)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ketang
{
class Program
{
static void Main(string[] args)
{
int a;
int b; //定义两个变量
for (a = 1; a <= 5; a++) //一个变量控制层数
{
for (b = 1; b<= a; b++) //一个变量控制输出*个数
{
Console.Write("*"); //输出*
}
Console.WriteLine(); //换行
}
}
}
}
3.打印数阵
1 2 3
1 2 3
1 2 3(for循环)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ketang
{
class Program
{
static void Main(string[] args)
{
int x;
int y;
for (x = 1; x <= 3; x++) // 循环变量行号
{
for (y = 1; y <= 3; y++) //循环变量列号
{
Console.Write(y); //第一行输出列号
Console.Write(" "); //输出空格隔开
}
Console.WriteLine(); //换行
}
}
}
}
4.打印数阵
11 12 13
21 22 23
31 32 33(for循环,运用占位符)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ketang
{
class Program
{
static void Main(string[] args)
{
int x;
int y;
for (x = 1; x <= 3; x++) // 循环变量行号
{
for (y = 1; y <= 3; y++) //循环变量列号
{
Console.Write("{0}{1}",x,y); //占位符输出行号和列号
Console.Write(" "); //输出空格隔开
}
Console.WriteLine(); //换行
}
}
}
}
5.计算方程x*x+y*y+2*x*y=25,x和y的值(for循环,运用占位符输出)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ketang
{
class Program
{
static void Main(string[] args)
{
int x;
int y; //定义两个变量(未知数)
for (x = 1; x <= 25; x++) //x的取值范围
{
for (y = 1; y <= 25; y++) //y的取值范围
{
if (x * x + y * y + 2 * x * y == 25) //方程等式的结果
{
Console.WriteLine("x={0},y={1}", x, y); //输出x,y分别的值
}
}
}
}
}
}
6.计算三个一位整数a,b,c分别作为个位十位百位,abc,cba相加等于1333(for循环,运用占位符输出)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ketang
{
class Program
{
static void Main(string[] args)
{
int a,b,c; //定义三个变量
for (a = 1; a <= 9; a++) //第一个未知数的取值
{
for (b = 1; b <= 9; b++) //第二个未知数的取值
{
for (c = 1; c <= 9; c++) //第三个未知数的取值
{
if(a*100+b*10+c+c*100+b*10+a==1333) //判断相加等于值
{
Console.WriteLine("a={0},b={1},c={2}",a,b,c); //输出a,b,c的值
}
}
}
}
}
}
}
7.在主方法同一个类中,判断两个数的奇偶性(方法)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ketang
{
class Program
{
static void Main(string[] args)
{
JiOuShu(6);
}
static void JiOuShu(int x)
{
if (x%2==0)
{
Console.WriteLine("{0}是偶数", x);
}
else
{
Console.WriteLine("{0}是奇数", x);
}
}
}
}
7.在主方法同一个类中,输入两个数求和(方法)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ketang
{
class Program
{
static void Main(string[] args)
{
int x, y; //定义两个变量
x = int.Parse(Console.ReadLine()); //输入第一个变量的值
y = int.Parse(Console.ReadLine()); //输入第二个变量的值
QiuHe(x, y); //存入定义的'QiuHe'里面
}
static void QiuHe(int x,int y)
{
Console.WriteLine("{0}加{1}等于{2}",x,y,x+y); //输出结果
}
}
}