从另一个类调用方法时出现问题
本文关键字:问题 方法 另一个 调用 | 更新日期: 2023-09-27 18:21:52
我在从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,否则您当前总是会看到"这是一个无效的选择"…因为最终的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
不是任何已知值,则使用"默认"分支。"
您的主要问题是其他人提到的关于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");
}
最初,您必须稍微更改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");
}
这样做,您将得到在所有情况下都知道的消息,其中x
不是4,在其他情况1、2和3中也是如此。
您的代码正在检查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;
}
问题在于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
为true,因此它会打印"这是一个无效的选择"。
以及关于您的实现的一些注意事项。。。
在主程序中,没有必要有Area
对象Area r = new Area()
,因为在任何地方都不使用r
。
Area
中的所有方法都接受值(您传入的值),但随后完全忽略它们并再次请求值。我要么从方法中删除参数,要么在主程序中询问用户并传递值。对于每个不同的计算,输入逻辑可以放在if
语句中。最后,您不会对函数的返回值执行任何操作,因此不会向用户显示区域。您需要编辑您的函数才能将其写入控制台,例如:
Console.WriteLine("The square area is {0}", Area.Square());
在if语句中,或者因为您在计算中进行用户输入,所以在每个Area
方法中都可以有一行类似的行。
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;
}
}
}
}