有问题从另一个类调用方法

时间:2015-11-13 13:26:04

标签: c#

我在C#中从另一个类调用方法时遇到问题。我没有做过这么长时间(确切地说是3周)并且没有得到调用方法背后的想法。我已经宣布一切都是公开的,试图让它变得更容易,但它还没有为我工作。任何帮助都将非常感谢。这是有问题的代码,我想在if语句中使用一个方法来计算各种简单形状的面积,但是在输出阶段我得到了#34;这不是一个有效的选择"

namespace Area_Calculator
{
    public class Area
    {
        public static int Square(int side)
        {
            int i, A;
            Console.WriteLine("Please enter the length of the side of the square");
            i = Convert.ToInt16(Console.ReadLine());
            A = i * i;
            return A;
        }
        public static int Rectangle(int width, int height)
        {
            int i, j, A;
            Console.WriteLine("Please enter the width of the rectangle");
            i = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("Please enter the height of the rectangle");
            j = Convert.ToInt16(Console.ReadLine());
            A = i * j;
            return A;
        }
        public static double Triangle(int width, int height)
        {
            double i, j, A;
            Console.WriteLine("Please enter the width of the triangle");
            i = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Please enter the height of the triangle");
            j = Convert.ToDouble(Console.ReadLine());
            A = (.5 * i * j);
            return A;
        }
        public static double Circle(int radius)
        {
            int i;
            double A;
            Console.WriteLine("Please enter the radius of the circle");
            i = Convert.ToInt16(Console.ReadLine());
            A = (i * Math.PI);
            return A;
        }

    }
    class Program
    {

        static void Main(string[] args)
        {
            int x, i, j;
            i = 0;
            j = 0;
            Console.WriteLine("Please select what type of shape you wish to find the area of:\n1. Square\n2. Rectangle\n3. Triangle\n4. Circle\n");
            x = Convert.ToInt16(Console.ReadLine());
            Area r = new Area();
            if (x == 1)
            {
                Area.Square(i);
            }
            if (x == 2)
            {
                Area.Rectangle(j, i);
            }
            if (x == 3)
            {
                Area.Triangle(j, i);
            }
            if (x == 4)
            {
                Area.Circle(i);
            }
            else
            {
                Console.WriteLine("That is an invalid choice");
            }
            Console.ReadKey();
        }
    }
}

6 个答案:

答案 0 :(得分:7)

你现在总是会看到“这是一个无效的选择”,除非x是4 ...因为 final if /`else与其他所有选项断开连接。

可以将其更改为在任何地方使用else if,如下所示:

if (x == 1)
{
    ...
}
else if (x == 2)
{
    ...
}
else if (x == 3)
{
    ...
}
else if (x == 4)
{
    ...       
}
else
{
    ...
}

...但使用switch语句会更简单:

switch (x)
{
    case 1:
        ...
        break;
    case 2:
        ...
        break;
    case 3:
        ...
        break;
    case 4:
        ...
        break;
    default:
        ...
        break;
}

如果x不是x,那么最好表达你的意图“我希望基于11-1234-1-1 or 11-1234-12-20 上的简单选择,使用”默认“分支执行其中一个分支。已知的价值观。“

答案 1 :(得分:2)

你的主要问题是,其他人提到的关于if语句的问题。另一件事是你计算面积但从未打印出来。

if (x == 1)
{
     Console.WriteLine(Area.Square(i));
} 
else if (x == 2)
{
    Console.WriteLine(Area.Rectangle(j, i));
}
else if (x == 3)
{
    Console.WriteLine(Area.Triangle(j, i));
}
else if (x == 4)
{
    Console.WriteLine(Area.Circle(i));
}
else
{
    Console.WriteLine("That is an invalid choice");
}

答案 2 :(得分:0)

问题在于您的if声明,您需要使用else if而不是if

if (x == 1)
{
    Area.Square(i);
}
else if (x==2)
{
    Area.Rectangle(j, i);
}
else if (x == 3)
{
    Area.Triangle(j, i);
}
else if (x==4)
{
    Area.Circle(i);
}
else
{
    Console.WriteLine("That is an invalid choice");
}

使用if时的问题是它到了最后一个if else是真的,所以它会打印“这是一个无效的选择”。

关于你的实施的一些说明......

在您的主程序中,由于您未在任何地方使用Area,因此无需拥有Area r = new Area()对象r

Area中的所有方法都会获取值(您传入的值),但随后会完全忽略它们并再次询问值。我要么从方法中删除参数,要么在主程序中询问用户并传入值。对于每个不同的计算,输入逻辑可以放在if语句中。最后,您不会对函数的返回值执行任何操作,因此您不会向用户显示该区域。您需要编辑函数以将其写入控制台,例如:

Console.WriteLine("The square area is {0}", Area.Square());

在if语句中,或者因为你在计算中进行用户输入,你可以在每个Area方法中使用类似的行。

答案 3 :(得分:0)

最初,你必须改变你的if语句,如下所示:

x

这样做,您将收到在所有情况下都知道的消息,其中{{1}}不是4,在其他情况下1,2和3也是如此。

答案 4 :(得分:0)

您的代码正在检查x是否等于4,否则使用else块中的代码。

每次都会运行,除非x = 4!

请改为尝试:

    x = Convert.ToInt16(Console.ReadLine());
    Area r = new Area();

    if (x == 1)
    {
        Area.Square(i);
    }
    else if (x==2)
    {
        Area.Rectangle(j, i);
    }
    else if (x == 3)
    {
        Area.Triangle(j, i);
    }
    else if (x==4)
    {
        Area.Circle(i);
    }
    else
    {
        Console.WriteLine("That is an invalid choice");
    }

甚至更好:

x = Console.ReadLine();

switch x
{
  case "1":
    Area.Square(i);
    break;

  case "2":
    Area.Rectangle(j, i);
    break;

  case "3":
    Area.Triangle(j, i);
    break;

  case "4":
    Area.Circle(i);
    break;

  default:
    console.WriteLine("That is an invalid choice");
    break;
}

答案 5 :(得分:0)

enum AreaEnum
{
    Square,
    Rectangle,
    Triangle,
    Circle,
};

class Program
{
    static void Main(string[] args)
    {


        Console.WriteLine("Please select what type of shape you wish to find the area of:\n1. Square\n2. Rectangle\n3. Triangle\n4. Circle\n");
        int x = Convert.ToInt16(Console.ReadLine());
        AreaEnum myValueAsEnum = (AreaEnum)x;
        Calculate(myValueAsEnum);

    }

    static double Calculate(AreaEnum a)
    {
        int x, i, j;
        i = 0;
        j = 0;
        Area area = new Area();

        switch (a)
        {
            case AreaEnum.Square:
                {
                 return Area.Square(i);
                }

            case AreaEnum.Rectangle:
                {
                    return Area.Rectangle(j, i);
                }

            case AreaEnum.Triangle:
                {
                    return Area.Triangle(j, i);
                }

            case AreaEnum.Circle:
                {
                    return Area.Circle(i);
                }

            default:
                {
                    Console.WriteLine("That is an invalid choice");
                    return 0;

                }
        }
    }


}