简体   繁体   English

C#中的乘法

[英]Multiplication in C#

I'm new to C# and tried the following program on multiplication using Switch. 我是C#的新手,并尝试过使用Switch使用以下程序进行乘法。 The program is perfectly going thru the Switch cases, but the multiplied result is not displaying in the console. 该程序可以完美地通过Switch案例,但是乘积结果不会显示在控制台中。 Can you please help: 您能帮忙吗?

using System;

public class Program
{
 public static void Main(string[] args)
 {
    int _pLicense = 50;
    int _sLicense = 100;
    int _eLicense = 150;
    String lType;
    int nSeats;
Console.WriteLine("\n1.Personal License\n2.Startup License\n3.Enterprise License");
        Console.WriteLine("Enter the license type");
        lType = Console.ReadLine();
        //lType = char.Parse(Console.ReadLine());


switch(lType)
    {
        case "1":
            Console.WriteLine("Enter the number of seats");
            nSeats = int.Parse(Console.ReadLine());
            //Console.WriteLine(_pLicense);
            int Cost = _pLicense*nSeats;
            Console.WriteLine("Personal License Cost : $", +Cost);
            break;
        case "2":
            Console.WriteLine("Enter the number of seats");
            nSeats = int.Parse(Console.ReadLine());
            //Console.WriteLine(_sLicense);
            Cost = (_sLicense * nSeats);
            Console.WriteLine("Startup License Cost : $", Cost);
            break;
        case "3":
            Console.WriteLine("Enter the number of seats");
            nSeats = int.Parse(Console.ReadLine());
            //Console.WriteLine(_pLicense);
            Cost = (_eLicense * nSeats);
            Console.WriteLine("Enterprise License Cost : $", Cost);
            break;
        default:
        Console.WriteLine("Invalid Type");
           break;
    }
}

} }

Change your Console.Writeline to Console.Writeline更改为

Console.WriteLine($"Personal License Cost : ${Cost}");

and

Console.WriteLine($"Enterprise License Cost : ${Cost}");

You can also format Cost using {Cost:d} see https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting . 您也可以使用{Cost:d}格式化“费用”,请参阅https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/composite-formatting

It is also advisable that you don't use int.Parse because it will throw an error if a character is entered. 还建议您不要使用int.Parse,因为如果输入字符,它将引发错误。 So change the code to something like 所以将代码更改为

if(int.TryParse(Console.ReadLine(), out int nSeats)
{
    Console.WriteLine($"Enterprise License Cost : ${Cost}");
}
else
{
    //Try again
}

You don't have to declare nSeats at the top, and it handles the error. 您不必在顶部声明nSeats,它可以处理错误。

This is how I would've write the code 这就是我编写代码的方式

public static void Main(string[] args)
{
    int nSeats = 0;
    Console.WriteLine("\n1.Personal License\n2.Startup License\n3.Enterprise License");
    Console.WriteLine("Enter the license type");
    var lType = Console.ReadLine();  //ReadLine returns a string, so it will automatically make lType a string
    int _LicenseCost = lType == "1" ? 50 : lType == "2" ? 100 : lType == "3" ? 150 : 0;
    if(_LicenseCost > 0)
    {
        while(true)
        {
            Console.WriteLine("Enter the number of seats");
            if(int.TryParse(Console.ReadLine(), out nSeats))
                break;
            else
                Console.WriteLine("Please enter a valid number");
        }
        Console.WriteLine((lType == "1" ? "Personal" : lType == "2" ? "Startup" : lType == "3" ? "Enterprise" : "") + $" License Cost : ${_LicenseCost * nSeats}");
    }
    else
        Console.WriteLine("License type is not valid. Good bye!");
}

For clarity, simply use the following: 为了清楚起见,只需使用以下内容:

var cost = (_sLicense * nSeats);
Console.WriteLine("Startup License Cost : " + cost);

您缺少格式化程序{0}

 Console.WriteLine("Enterprise License Cost : ${0}", Cost);

Your are missing code to displaying actual result. 您缺少显示实际结果的代码。 You can try this: 您可以尝试以下方法:

Console.WriteLine("Personal licence : " + cost);

etc. 等等

Firstly, declare your Cost variable outside the switch statement. 首先,在switch语句之外声明Cost变量。 You can declare it along with your other variables. 您可以将其与其他变量一起声明。 Secondly, assign and display the Cost variable in each case of the switch statement. 其次,在switch语句的每种情况下,分配并显示Cost变量。

using System;

public class Program
{
 public static void Main(string[] args)
 {
    int _pLicense = 50;
    int _sLicense = 100;
    int _eLicense = 150;
    String lType;
    int nSeats;
    int Cost;

Console.WriteLine("\n1.Personal License\n2.Startup License\n3.Enterprise License");
        Console.WriteLine("Enter the license type");
        lType = Console.ReadLine();
        //lType = char.Parse(Console.ReadLine());


switch(lType)
    {
        case "1":
            Console.WriteLine("Enter the number of seats");
            nSeats = int.Parse(Console.ReadLine());
            //Console.WriteLine(_pLicense);
            Cost = _pLicense*nSeats;
            Console.WriteLine("Personal License Cost : $" + Cost);
            break;
        case "2":
            Console.WriteLine("Enter the number of seats");
            nSeats = int.Parse(Console.ReadLine());
            //Console.WriteLine(_sLicense);
            Cost = (_sLicense * nSeats);
            Console.WriteLine("Startup License Cost : $" + Cost);
            break;
        case "3":
            Console.WriteLine("Enter the number of seats");
            nSeats = int.Parse(Console.ReadLine());
            //Console.WriteLine(_pLicense);
            Cost = (_eLicense * nSeats);
            Console.WriteLine("Enterprise License Cost : $" + Cost);
            break;
        default:
        Console.WriteLine("Invalid Type");
           break;
    }
}

String formatting example: 字符串格式示例:

string name = "Fred";
String.Format("Name = {0}, hours = {1:hh}", name, DateTime.Now);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM