你能在课堂上有if语句吗?

时间:2016-12-06 19:05:18

标签: c#

嘿,我对c sharp和编码相对较新,我正在尝试创建一个我可以调用并稍后打印出来的类。我正在尝试创建的类包含一个简单的if语句,例如,如果您的汽车发出如此多的排放,您需要支付一定的价格。我的问题是无法在类中创建if然后再调用它。这是否可能,如果不是任何帮助非常感激

 public double emissions;
 public double carTax;

 public double ifCarTax(double emissions)
        {
            carTax = if (emissions == 0)
            {
                Console.WriteLine("Car tax is 120");
            }
            else if (emissions >= 1 && emissions <= 80)
            {
                Console.WriteLine("Car tax is 170");
            }
            else if (emissions >= 81 && emissions <= 100)
            {
                Console.WriteLine("Car tax is 180");
            }
            else if (emissions >= 101 && emissions <= 110)
            {
                Console.WriteLine("Car tax is 190");
            }
            else if (emissions >= 111 && emissions <= 120)
            {
                Console.WriteLine("Car tax is 200");
            }
            else if (emissions >= 121 && emissions <= 130)
            {
                Console.WriteLine("Car tax is 270");
            }
            else    //Catch invalid input
            {
                Console.WriteLine("Invalid CO2 emissions");
            }

        }

        public double getCarTax()
        {
            return ifCarTax;
        }

1 个答案:

答案 0 :(得分:1)

这里是班级的基本结构...... 如果您发布实际代码,我们就能告诉您出错的确切位置。

using System;

class MyClassName
{
    static void YourMethodName(string incomingString1, string incomingString2)
    {
        if (1 == 1)
        {
            //do something
        }
        else
        {
            //do something different
        }
    }
}

将提供的代码重构为:

using System;

namespace _41002924
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(CarTax.getCarTax(100));
            Console.ReadLine();
        }
    }

    public static class CarTax
    {
        public static double emissions { get; set; }
        public static double carTax { get { return getCarTax(emissions); } }

        public static double getCarTax(double emissions)
        {
            if (emissions == 0)
            {
                Console.WriteLine("Car tax is 120");
                //carTax = 120;
                return 120;
            }
            else if (emissions >= 1 && emissions <= 80)
            {
                Console.WriteLine("Car tax is 170");
                //carTax = 170;
                return 170;
            }
            else if (emissions >= 81 && emissions <= 100)
            {
                Console.WriteLine("Car tax is 180");
                //carTax = 180;
                return 180;
            }
            else if (emissions >= 101 && emissions <= 110)
            {
                Console.WriteLine("Car tax is 190");
                //carTax = 190;
                return 190;
            }
            else if (emissions >= 111 && emissions <= 120)
            {
                Console.WriteLine("Car tax is 200");
                //carTax = 200;
                return 200;
            }
            else if (emissions >= 121 && emissions <= 130)
            {
                Console.WriteLine("Car tax is 270");
                //carTax = 270;
                return 270;
            }
            else    //Catch invalid input
            {
                Console.WriteLine("Invalid CO2 emissions");
                //carTax = -999;
                return -999;
            }
        }
    }
}