C#第十六节课
out
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @out
{
class Program
{
//传值
//传址
public void jia(int x)
{
x = x + 3;
Console.WriteLine("函数里a=" + x);
}
//传值,只将这个变量的值给拿走,不返还,除非return赋值。
//将a的值传入函数,无论这个只在函数中如何变化,不会影响Main中的a变化
//传址,将这个变量的值拿走运算,完成后还是得返还回来(不用return)。
//将a的值传入函数,函数中这个值如何变化,main中的a也会跟着变化
//相当于:函数中的这个值,是main中这个a的快捷方式
//out传值(传址)
public void jia(out int a, out int sum)
{
a = 3;
sum = 5;
sum = sum + a;
a += sum;
}
public void fangcheng(out double x1, out double x2, out double a, out double b, out double c, out double d)
{
d = -1;
x1 = 0;
x2 = 0;
Console.Write("请输入a:");
a = double.Parse(Console.ReadLine());
Console.Write("请输入b:");
b = double.Parse(Console.ReadLine());
Console.Write("请输入c:");
c = double.Parse(Console.ReadLine());
if (a == 0 && b == 0 && c == 0)
{
Console.WriteLine("有无数解");
}
else if (a == 0 && b == 0)
{
Console.WriteLine("无解");
}
else if (a == 0)
{
Console.WriteLine("此为一元一次方程");
x1 = c / b;
x2 = c / b;
}
else
{
d = ((b * b) - (4 * a * c));
if (d == 0)
{
x1 = (-b) / (2 * a);
x2 = (-b) / (2 * a);
Console.WriteLine("此函数只有一个根");
}
else if (d > 0)
{
x1 = ((-b) + Math.Sqrt(d)) / (2 * a);
x2 = ((-b) - Math.Sqrt(d)) / (2 * a);
Console.WriteLine("此函数有两个根");
}
else
{
Console.WriteLine("此函数无解");
}
}
}
static void Main(string[] args)
{
for (; ; )
{
//int a;
//int sum;
Program pp = new Program();
//pp.jia(out a,out sum);
//Console.WriteLine(sum);
//Console.WriteLine(a);
//Console.ReadLine();
double d = 0;
double x1 = 0;
double x2 = 0;
double a = 0;
double b = 0;
double c = 0;
pp.fangcheng(out x1, out x2, out a, out b, out c, out d);
if (d > 0)
{
Console.WriteLine("x1等于:" + x1);
Console.WriteLine("x2等于:" + x2);
Console.WriteLine("a等于:" + a);
Console.WriteLine("b等于:" + b);
Console.WriteLine("c等于:" + c);
}
else if (a == 0 && b != 0 || d == 0)
{
Console.WriteLine("x等于:" + x1);
}
else
{
}
Console.ReadLine();
Console.Clear();
//Program pp = new Program();
//int a = 3;
//pp.jia(a);
//Console.WriteLine("main中a="+a);
//Console.ReadLine();
}
}
}
}