c#小程序

来源:百度知道 编辑:UC知道 时间:2024/06/04 05:45:10
using System;
using System.Collections.Generic;
using System.Text;

public class tuxing
{
interface share
{
double zch();
double mj();
}
class juxing : share
{
public int x;
public int y;

public juxing(int x, int y)
{
this.x = x;
this.y = y;
}

public double zch()
{
int zch;
zch = (x + y) * 2;
return zch;
}
public double mj()
{
int mj;
mj = x * y;
return mj;
}
}
class yuanxing : share

{
private double x;
private int y;

public yuanxing(double x, int y)
{
this.x = x;
this.y = y;

public sanjiaoxing(int x, int y, int z)
{
if(x+y<=z||x+z<=y||y+z<=x)
throw new Exception("not valid data");
this.x = x;
this.y = y;
this.z = z;
}

//...

switch (n)
{
case "a":
s = new juxing(5, 7);
break;
case "b":
s = new yuanxing(3.14, 2);
break;
case "c":
try
{
s = new sanjiaoxing(2, 5,8);
}
catch(Exception e)
{
Console.WriteLine(e.toString());
}
break;
default:
Console.WriteLine("特殊情况,输入的字母不对。");
break;
}

你要写一个方法来判断啊,

public sanjiaoxing(int x, int y, int z)
{
if(x+y>z&&x-y<z&&z+z>y&&x-z<y&&y+z>x&&y-z<x&&z-y<x)
{
this.x = x;
this.y = y;
this.z = z;
}else
Console.WriteLine("不是三角形");
}

另外try catch不是用在逻辑错误,而是运行时错误,你自己的逻辑算法要你自己写方法判断