我是C#的新手,并尝试使用Switch使用以下程序进行乘法。该程序可以完美地通过Switch案例,但是乘积结果不会显示在控制台中。您能帮忙吗?
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;
}
}
}
答案 0 :(得分:6)
将您的Console.Writeline
更改为
Console.WriteLine($"Personal License Cost : ${Cost}");
和
Console.WriteLine($"Enterprise License Cost : ${Cost}");
您还可以使用{Cost:d}
参见https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting格式化“费用”。
也建议您不要使用int.Parse,因为如果输入字符,它将引发错误。因此,将代码更改为
if(int.TryParse(Console.ReadLine(), out int nSeats)
{
Console.WriteLine($"Enterprise License Cost : ${Cost}");
}
else
{
//Try again
}
您不必在顶部声明nSeats,它可以处理错误。
这就是我编写代码的方式
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!");
}
答案 1 :(得分:2)
为清楚起见,只需使用以下内容:
var cost = (_sLicense * nSeats);
Console.WriteLine("Startup License Cost : " + cost);
答案 2 :(得分:1)
您缺少格式化程序,如{0}
Console.WriteLine("Enterprise License Cost : ${0}", Cost);
答案 3 :(得分:0)
您缺少显示实际结果的代码。您可以尝试以下方法:
Console.WriteLine("Personal licence : " + cost);
等
答案 4 :(得分:0)
首先,在switch语句之外声明Cost变量。您可以将其与其他变量一起声明。 其次,在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;
}
}
答案 5 :(得分:0)
字符串格式示例:
string name = "Fred";
String.Format("Name = {0}, hours = {1:hh}", name, DateTime.Now);