【发布时间】:2020-02-22 20:28:27
【问题描述】:
所以我有这个代码,您可以在其中输入“区号”,然后输入您希望通话时长。这基本上是一个简单的计算器,可以根据您的区号计算通话费用。如果我输入了无效的区号,我很难弄清楚如何保持循环运行。到目前为止,如果我输入了无效的区号,整个程序只会在命令提示符下结束。代码如下:
using System;
using static System.Console;
namespace Chapter6._1
{
class Program
{
static void Main()
{
// array info //
int[] phoneAreacode = { 608, 414, 262, 815, 715, 920 };
double[] phoneCost = { .05, .10, .07, .24, .16, .14 };
// declaring variables //
int x;
int areaCode;
double cost = 0;
int callLength;
bool validAreacode = false;
// start of actual code //
Write("Enter in the area code you want to call: ");
areaCode = Convert.ToInt32(ReadLine());
x = 0;
while (x < phoneAreacode.Length && areaCode != phoneAreacode[x])
++x;
if(x != phoneAreacode.Length)
{
validAreacode = true;
cost = phoneCost[x];
}
if (validAreacode)
{
Write("Enter in the length of your call: ");
callLength = Convert.ToInt32(ReadLine());
double finalCost = callLength * cost;
WriteLine("Your call to area code " + areaCode + " for " + callLength + " minutes will cost " + finalCost.ToString("C"));
}
else
{
WriteLine("YOU MUST ENTER A VALID AREA CODE!");
}
}
}
}
【问题讨论】:
-
我会为此而大喊大叫,但只需在
Enter in the area code之前使用标签myLabel:。然后在“您必须输入有效的...”之后使用goto myLabel
标签: c# error-handling while-loop