c# - 从另一个类调用方法时遇到问题

标签 c#

我在使用 C# 调用另一个类的方法时遇到问题。我并没有做这么长时间(准确地说是 3 周),也不了解调用方法背后的想法。我已将所有内容都宣布为公开以尝试使其更容易,但它对我来说还不起作用。任何帮助都将不胜感激。这是有问题的代码,我想在 if 语句中使用一种方法来计算各种简单形状的面积,但是在输出阶段我得到“这不是一个有效的选择”

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();
        }
    }
}

最佳答案

你目前总是会看到“这是一个无效的选择”,除非 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 则使用“默认”分支不是任何已知值。”

关于c# - 从另一个类调用方法时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33693791/

相关文章:

c# - 如何将 .xml 文件与 xmlns 属性一起反序列化?

C# string.IsNullOrWhiteSpace ("\t") == true

c# - 使用数组在 C# 中进行数据驱动测试

c# - 以编程方式创建数据透视项并动态添加堆栈面板?

c# - 使用 C# 的 Azure 文本分析给出错误

C# 代码显示错误 "A namespace cannot directly contain members such as fields or methods"

c# - Linq:在已经连接的表上包含一个连接

用于克隆 IList 的 C# 通用扩展方法

c# - .Net 5 - Asp.Net 仍然没有 SynchronizationContext?

c# - 数据表序列化没有按预期工作