C# 错误';程序编号';是一个';类型';但是像';变量
错误“Program.Number”是“类型”,但与“变量”一样使用 当我试着运行这个程序的时候,我不断地得到上面的错误,我做错了什么C# 错误';程序编号';是一个';类型';但是像';变量,c#,C#,错误“Program.Number”是“类型”,但与“变量”一样使用 当我试着运行这个程序的时候,我不断地得到上面的错误,我做错了什么 using System; class Program { enum Number{ standard = 1, express = 2, same = 3}; const int A = 1, B = 2; const int Y = 3, N = 4; static void Main() {
using System;
class Program
{
enum Number{ standard = 1, express = 2, same = 3};
const int A = 1, B = 2;
const int Y = 3, N = 4;
static void Main()
{
double cost, LB;
int numValues, Number_of_items ;
Console.WriteLine("please enter the type of shiping you want");
Console.WriteLine("Enter 1:standard shipping.");
Console.WriteLine("Enter 2:express shipping.");
Console.WriteLine("Enter 3:same day shipping.");
switch ((Number))
{
case Numbers.standard:
Console.WriteLine("thankyou for chooseing standerd shipping");
Console.WriteLine("please choose a catagory");
Console.Write("Type A or B to make your selection");
{ if (A==A)
{
Console.Write("please enter the number of items");
Number_of_items = int.Parse(Console.ReadLine());
cost = 3 * Number_of_items;
Console.Write("is this shipment going to alaska or Hawaii? (y or n)");
if (Y==Y)
{
cost = cost + 2.50;
Console.WriteLine("Total cost is {0}." , cost);
}
else
Console.WriteLine("total cost is {0}." , cost);
}
else
Console.Write("please enter the weiht in pounds");
LB = double.Parse(Console.ReadLine());
cost = 1.45 * LB;
Console.WriteLine("is this shipment going to alaska or Hawaii? (y or n)");
}
if (Y==Y)
{
cost = cost + 2.50;
Console.WriteLine("Total cost is {0}." , cost);
}
else
Console.WriteLine("total cost is {0}." , cost);
break;
case Numbers.express:
Console.WriteLine("thankyou for chooseing Express Shipping");
Console.WriteLine("please choose a catagory");
Console.Write("Type A or B to make your selection");
{ if (A==A)
Console.Write("please enter the number of items");
Number_of_items = int.Parse(Console.ReadLine());
cost = 4 * Number_of_items;
{
Console.Write("is this shipment going to alaska or Hawaii? (y or n)");
if (Y==Y)
{
cost = cost + 5.00;
Console.WriteLine("Total cost is {0}." , cost);
}
else
Console.WriteLine("total cost is {0}." , cost);
}
if(B==B)
Console.Write("please enter the weiht in pounds");
LB = double.Parse(Console.ReadLine());
cost = 2.50 * LB;
Console.WriteLine("is this shipment going to alaska or Hawaii? (y or n)");
}
if (Y==Y)
{
cost = cost + 5.00;
Console.WriteLine("Total cost is {0}." , cost);
}
else
Console.WriteLine("total cost is {0}." , cost);
break;
case Numbers.same:
Console.WriteLine("thankyou for chooseing Same Day Shipping");
Console.WriteLine("please choose a catagory");
Console.Write("Type A or B to make your selection");
if (A == A)
Console.Write("please enter the number of items");
Number_of_items = int.Parse(Console.ReadLine());
cost = 5.50 * Number_of_items;
Console.Write("is this shipment going to alaska or Hawaii? (y or n)");
if (Y==Y)
{
cost = cost + 8.00;
Console.WriteLine("Total cost is {0}." , cost);
}
else
Console.WriteLine("total cost is {0}." , cost);
if (B==B)
Console.Write("please enter the weiht in pounds");
LB = double.Parse(Console.ReadLine());
cost = 3.00 * LB;
Console.WriteLine("is this shipment going to alaska or Hawaii? (y or n)");
if (Y==Y)
{
cost = cost + 8.00;
Console.WriteLine("Total cost is {0}." , cost);
}
else
Console.WriteLine("total cost is {0}." , cost);
break;
}
numValues = 1;
Console.ReadLine();
}//End Main()
}//End class Program
错误在这里:
switch((Number))
我想你是想给编号下点什么,但忘了。我想你的意思是:
int input;
while(!int.TryParse(Console.ReadLine(), ref input) || input < 1 || input > 3) {
Console.WriteLine("Please enter a valid option!");
}
switch((Number)input)
int输入;
而(!int.TryParse(Console.ReadLine(),ref-input)| | input<1 | | input>3){
WriteLine(“请输入有效选项!”);
}
开关((数字)输入)
如果要打开类型编号
,则必须使用编号的实例。
例如:
switch ((Number)myNumber)
嗯,编译器是正确的(通常情况下也是如此)。。。在
开关
中,您认为您从何处获得测试值?枚举数{standard=1,express=2,same=3};这是我想得到它的地方,但这是我第一次使用开关,我有点不知所措,因为这行定义了一个enum
——这只是一些方便的整数名称;它没有获得任何东西的值AllRight,那么我从这里到哪里去呢?我希望他们能够输入数字1-3并转到那个案例看看@minitech answer从控制台读取输入OK,但是我在代码中把我的数字放在哪里,比如常量或int?你必须给变量myNumber
赋值。如果它的类型是Number
你不必在开关中强制转换它。好吧,我给它分配了一个值,但现在我被困在案例1(我分配的值)中。我如何设置它,以便它可以通过开关var myNumber=(Number)int.Parse(Console.ReadLine())向用户请求该值
好的,我修复了这部分代码,感谢您的快速响应