C#编程(3_流程控制)
2020-12-13 13:59
标签:style blog color io ar 使用 for sp div Mandelbrot集合中的每个位置都对应于公式N=x+y*i中的一个复数。实数部分是x,虚数部分是y,i是-1的平方根。图像中的各个位置的x和y坐标对应于复数的x和y部分。图像中的每个位置用参数N来表示,它是x*x+y*y的平方根。如果这个值大于或者等于2,则这个数字对应的位置是0。如果这个参数小于2,就把N的值改为N*N-N(即N=(x*x-y*y-x)+(2*x*y-y)*i),并再次测试这个新的N值。如果这个值大于或者等于2,则这个值对应位置是1.这个过程将一直继续下去,直到给图像中的位置赋一个值,或迭代执行的次数超过指定的次数为止。根据给图像中每个点赋予的值,在图形环境下,屏幕上会显示某种颜色的像素。但是本例使用的文本环境,所以屏幕上显示的是一个字符。 其中计算 举例: 把那个字符串赋给resultString,取决于myInterger的值与10的比较。如果myInterger的值小于10,就把第一个字符串赋给resultString;如果myInterger的值大于或者等于10,就把第二个字符串赋给resultString。这个运算符比较适用于这样简单赋值语句,但不适用于根据比较结果执行大量代码的情形。此时应该使用if语句。 C#编程(3_流程控制) 标签:style blog color io ar 使用 for sp div 原文地址:http://www.cnblogs.com/haizhibin1989/p/4051345.html
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please Enter an integer:");
int myInt=Convert.ToInt32(Console.ReadLine());
bool isLessThan10 = myInt 10;
bool isBetweenThan0And5 = (myInt >= 0) && (myInt 5);
Console.WriteLine("Integer is less than 10?{0}",isLessThan10);
Console.WriteLine("Integer between 0 and 5?{0}",isBetweenThan0And5);
Console.WriteLine("Exactly one of the above is true?{0}",isLessThan10^isBetweenThan0And5);//对两个变量执行逻辑异或操作
Console.ReadKey();
}
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string comparison;
Console.WriteLine("Enter a number:");
double var1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter anther number:");
double var2 = Convert.ToDouble(Console.ReadLine());
if (var1 var2)
comparison = "less than";
else
{
if (var1 == var2)
comparison = "equal to";
else
comparison = "greater than";
}
Console.WriteLine("The frist number is {0} the second number.",comparison);
Console.ReadKey();
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const string myName = "karli";
const string sexyName = "angelina";
const string sillyName = "ploppy";
string name;
Console.WriteLine("What is you name?");
name = Console.ReadLine();
switch (name.ToLower())//name.ToLower()是一个标准命令,用于处理所有输入的字符串,使输入的字符串与与给定字符串匹配。
{
case myName:
Console.WriteLine("You have the same name with me!");
break;
case sexyName:
Console.WriteLine("my,What a sexy name you have!");
break;
case sillyName:
Console.WriteLine("That‘s a very ploppy name.");
break;
}
Console.WriteLine("Hello {0}!",name);
Console.ReadKey();
}
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//代码利用固定的利率,对年度计算余额的过程重复必要的次数,知道满足临界条件为止,在每次循环中,递增一个计数器变量,就可以确定需要多少年:
double balance, interestRate, targerBalance;
Console.WriteLine("What is your current balance?");
balance = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("What is your current annual interest rate(in %)?");
interestRate = 1 + Convert.ToDouble(Console.ReadLine())/100.0;
Console.WriteLine("What balance would you like to have?");
targerBalance = Convert.ToDouble(Console.ReadLine());
int totalYears = 0;
do
{
balance *= interestRate;
++totalYears;
}
while (balance targerBalance);
Console.WriteLine("In {0} year {1} you‘ll have the blance of {2}.",totalYears,totalYears==1?"":"s",balance);//三元运算符
Console.ReadKey();
}
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double balance, interestRate, targerBalance;
Console.WriteLine("What is your current balance?");
balance = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("What is your current annual interest rate(in %)?");
interestRate = 1 + Convert.ToDouble(Console.ReadLine()) / 100.0;
Console.WriteLine("What balance would you like to have?");
targerBalance = Convert.ToDouble(Console.ReadLine());
int totalYears = 0;
while (balance targerBalance)
{
balance *= interestRate;
++totalYears;
}
Console.WriteLine("In {0} year {1} you‘ll have the blance of {2}.", totalYears, totalYears == 1 ? "" : "s", balance);//三元
if (totalYears == 0)
Console.WriteLine("To be honest,you realy didn‘t need to use this calculator.");
Console.ReadKey();
}
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//(Mandelbrot)曼德布洛特集合
double realCoord, imagCoord;
double realTemp, imagTemp, realTemp2, arg;
int iterations;
for (imagCoord = 1.2; imagCoord >= -1.2; imagCoord -= 0.05)
{
for (realCoord = -0.6; realCoord 1.77; realCoord += 0.03)
{
iterations = 0;
realTemp = realCoord;
imagTemp = imagCoord;
arg = (realCoord * realCoord) + (imagCoord * imagCoord);
while ((arg 4) && (iterations 40))
{
realTemp2 = (realTemp * realTemp) - (imagTemp * imagTemp) - realCoord;
imagTemp = (2 * realTemp * imagTemp) - imagCoord;
realTemp = realTemp2;
arg = (realTemp * realTemp) + (imagTemp * imagTemp);
iterations += 1;
}
switch (iterations % 4)
{
case 0:
Console.Write(".");
break;
case 1:
Console.Write("o");
break;
case 2:
Console.Write("O");
break;
case 3:
Console.Write("@");
break;
}
}
Console.Write("\n");
}
Console.ReadKey();
}
}
}
string resultString=(myInterger10) ? "Less than 10":"Greater than or equal to 10";
上一篇:win7 rdp 8.1升级方法