程序编译器不允许我使用'else&;在最后两种情况下,我不知道为什么
本文关键字:两种 最后 情况下 为什么 我不知道 else 允许我 编译器 程序 不允许 | 更新日期: 2023-09-27 18:10:11
我不知道为什么案例2,3不允许我使用else进行类别选择,但它允许我在案例1中使用它。它在哪里崩溃了?哪些代码需要编辑?
我修复了我认为的其他问题,但我得到了一个新的错误,我以前从未见过的控制不能从一个case标签('case3:')到另一个。这是什么意思?
using System;
class Program
{
enum Numbers { 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 Number_of_items;
int myNumbers;
char catagory;
char surcharge = 'Y';
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.");
myNumbers = int.Parse(Console.ReadLine());
switch ((Numbers)myNumbers)
{
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");
catagory = char.Parse(Console.ReadLine());
{
if (catagory == '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)");
surcharge = char.Parse(Console.ReadLine());
if (surcharge == '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 weight in pounds");
LB = double.Parse(Console.ReadLine());
cost = 1.45 * LB;
Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)");
} surcharge = char.Parse(Console.ReadLine());
if (surcharge == '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");
catagory = char.Parse(Console.ReadLine());
if (catagory == '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)");
surcharge = char.Parse(Console.ReadLine());
if (surcharge == 'Y')
{
cost = cost + 5.00;
Console.WriteLine("Total cost is {0}.", cost);
}
else
Console.WriteLine("total cost is {0}.", cost);
}
}
else
{
{
Console.Write("please enter the weight in pounds");
LB = double.Parse(Console.ReadLine());
cost = 2.50 * LB;
Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)");
}
}
surcharge = char.Parse(Console.ReadLine());
if (surcharge == '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");
catagory = char.Parse(Console.ReadLine());
if (catagory == '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)");
surcharge = char.Parse(Console.ReadLine());
if (surcharge == 'Y')
{
{
cost = cost + 8.00;
Console.WriteLine("Total cost is {0}.", cost);
}
}
else
{
Console.WriteLine("total cost is {0}.", cost);
}
}
else
{
{
Console.Write("please enter the weight in pounds");
LB = double.Parse(Console.ReadLine());
cost = 3.00 * LB;
surcharge = char.Parse(Console.ReadLine());
Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)");
}
if (surcharge == 'Y')
{
cost = cost + 8.00;
Console.WriteLine("Total cost is {0}.", cost);
}
else
Console.WriteLine("total cost is {0}.", cost);
break;
}
Console.ReadLine();
}//End Main()
}
}
我试图通过Lindent
运行你的代码来修复可怕的格式,它报告了unmatched else
。你的代码中有太多的else
语句:
indent: foo.c:93: Error:Unmatched 'else'
indent: foo.c:139: Error:Unmatched 'else'
indent: foo.c:164: Error:Unexpected end of file
(是的,indent(1)
不是为c#设计的,但它在大多数类C语言上出奇地好,在这种情况下,它指出了至少两个else
语句没有正确匹配。不要担心那些特定的行号——这个例程需要正确格式化并分解成更小的部分——试图仅仅将它与一些正确放置的{}
拼凑在一起并不是正确的答案。
将庞大的main()
例程分解为更小的函数。(您的代码将阿拉斯加/夏威夷问题重复了6次——这应该是一个单独的例程。也许所有的是/否问题都应该通过一个例程来处理:传递问题,它会执行提示并为您返回true
或false
。
如果你在if
子句中使用大括号,我强烈建议你在else
子句中使用大括号:
if (foo)
/* statement */
else
/* single statement */
但是如果你曾经需要在第一个块上使用大括号,那么在else
块上也使用它们:
if (foo) {
/* stuff */
/* more stuff */
} else {
/* use those braces! */
/* you will find reading your code far easier if you do */
}
好运。
这里有两个问题。第一个是一对不应该在那里的括号。即:
catagory = char.Parse(Console.ReadLine());
{ // <------- THIS ONE
if (catagory == 'A')
//
// skipped code
//
Console.WriteLine("is this shipment going to alaska or Hawaii? (Y or N)");
} /* <------- THIS ONE */ surcharge = char.Parse(Console.ReadLine());
if (surcharge == 'Y')
第二个问题(导致控件无法通过错误的问题)是number的break语句。同样的情况是在语句的条件块内(在if (catagory == 'A')
的else部分)。这意味着它将只在类别不同于'A'时执行,而不是对变量的每个值执行。你只需要移动大括号后面的break。