第十三节 流程控制 三
输入一个数判断奇,偶数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Jiou
{
class Program
{
static void Main(string[] args)
{
int a;
Console.WriteLine("请输入一个数");
a=Convert.ToInt32(Console.ReadLine());
if (a % 2 == 0)
{
Console.WriteLine("输入的数是偶数");
}
else
Console.WriteLine("输入的数是奇数");
}
}
}
构成三角形
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace panduan
{
class Program
{
static void Main(string[] args)
{
double a, b, c;
Console.WriteLine("输入三个正实数");
a = Convert.ToDouble(Console.ReadLine());
b = Convert.ToDouble(Console.ReadLine());
c = Convert.ToDouble(Console.ReadLine());
if ((a + b > c) && (a + c > b) && (b + c > a))
{
Console.WriteLine("可以构成三角形");
}
else
{
Console.WriteLine("不能构成三角形");
}
}
}
}
选择结构(if语句)根据条件进行抉择的逻辑结构。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XuanZe
{
class Program
{
static void Main(string[] args)
{
double x;
Console.WriteLine("x=");
x = Convert.ToDouble(Console.ReadLine());
if (x >= 0)
{
double y = x + 2;
Console.WriteLine(y);
}
else
{
double y = x * x;
Console.WriteLine(y);
}
}
}
}
求绝对值,单分支语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace jueduizhi
{
class Program
{
static void Main(string[] args)
{
double x;
x = Convert.ToDouble(Console.ReadLine());
if (x < 0)
{
x = -x;
}
Console.WriteLine(x);
}
}
}
多分支选择结构
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ChengJi
{
class Program
{
static void Main(string[] args)
{
double a;
a = Convert.ToDouble(Console.ReadLine());
if ((a >= 90) && (a <= 100))
{
Console.WriteLine("优秀");
}
else if ((a < 90) && (a >= 80))
{
Console.WriteLine("良");
}
else if ((a < 80) && (a >= 60))
{
Console.WriteLine("合格");
}
else if ((a < 60) && (a >= 0))
{
Console.WriteLine("不合格");
}
else
Console.WriteLine("请重新输入");
}
}
}
嵌套选择结构
求一元二次方程ax2+bx+c = 0的根。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QianTao
{
class Program
{
static void Main(string[] args)
{
double a, b, c;
Console.WriteLine("请输入三个数");
a = Convert.ToDouble(Console.ReadLine());
b = Convert.ToDouble(Console.ReadLine());
c = Convert.ToDouble(Console.ReadLine());
double delta;
delta = b * b - 4 * a * c;
if (delta >= 0)
{
if (delta > 0)
{
double x1=(-b+Math.Sqrt(delta)/(2*a));
double x2 = (-b-Math.Sqrt(delta)/ (a*2));
Console.WriteLine("x1={0},x2={1}",x1,x2);
}
else
{
double x = -b / 2 * a;
Console.WriteLine("x={0}",x);
}
}
else
{
Console.WriteLine("无实根");
}
}
}
}
浙公网安备 33010602011771号