【问题标题】:Getting Error while using Math.Sqrt function in C#在 C# 中使用 Math.Sqrt 函数时出错
【发布时间】:2017-08-22 16:00:57
【问题描述】:

在下面给出的程序中,Math.Sqrt 函数抛出了一个错误,即 “表达式表示预期的 variable', where amethod 组。” 这里似乎有什么问题?


using System;
class program{
    static void Main(){
        Console.WriteLine("Enter the sides(a,b,c) of a triangle  :");
        int a = Convert.ToInt16(Console.ReadLine());
        int b = Convert.ToInt16(Console.ReadLine());
        int c = Convert.ToInt16(Console.ReadLine());
        double s = (a+b+c)/2;     
        double area = Math.Sqrt(s(s-a)(s-b)(s-c));
        if (a==b&&b==c){
            Console.WriteLine("This is an Equilateral trangle");
        }
        else if(a==b&&b!=c||a!=b&&b==c){
            Console.WriteLine("This is an Isosceles trangle");
        }
        else{
            Console.WriteLine("This is an Scalene trangle");
        }
    }
}here

【问题讨论】:

  • s(s-a)(s-b)(s-c) 语法无效。
  • 你的意思是把你的表达式写成s*(s-a)*(s-b)*(s-c)吗? C# 不会假设乘法。
  • double s = (a + b + c) / 2.0; 请注意.0

标签: c# math.sqrt


【解决方案1】:

C# 不会像你写方程那样假设乘法,而是将s(s-a) 视为一个名为s 的函数,它采用s-a 的参数。您需要明确说明乘号:

double area = Math.Sqrt(s*(s-a)*(s-b)*(s-c));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多