【问题标题】:Question about executing a while loop in C#关于在 C# 中执行 while 循环的问题
【发布时间】: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


【解决方案1】:

您可以将 Read 和 Check 例程包装到另一个 while 循环中:

bool validAreacode = false;

while(!validAreacode)
{
    // 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];
    }
    else
    {
        WriteLine("YOU MUST ENTER A VALID AREA CODE!");
    }
}

这对您来说是最简单的解决方案(不需要对您的代码进行太多更改)。但是您的代码仍然存在问题。如果用户尝试打印任何非数字字符而不是区号,您的程序将崩溃。

【讨论】:

    【解决方案2】:

    您可以在此处发送do-While

    基本上,当您执行do-while 时,您会强制执行do 上的代码,直到完成while 中的条件。在您的情况下,您需要在 do 语句中添加对 pohne 编号的检查,并且要知道该人是否选择了正确的值,您可以执行 Array.FindIndex(): 这将是您的do-while 循环,我也将您的x 更改为index 尝试使用具有某些含义的变量的名称。 (无论如何索引并不完美)

    do
    {
        Write("Enter in the area code you want to call: ");
        areaCode = Convert.ToInt32(ReadLine());
    
        index = Array.FindIndex(phoneAreacode, w => w == areaCode);
        if (index >= 0)
        {
            validAreacode = true;
        }
        else
        {
            WriteLine("YOU MUST ENTER A VALID AREA CODE!");
        }
    
    } while (!validAreacode);
    

    这将是你的整个主要方法:

    int[] phoneAreacode = { 608, 414, 262, 815, 715, 920 };
    double[] phoneCost = { .05, .10, .07, .24, .16, .14 };
    // declaring variables //
    int index;
    int areaCode;
    int callLength;
    bool validAreacode = false;
    // start of actual code //
    
    
    
    do
    {
        Write("Enter in the area code you want to call: ");
        areaCode = Convert.ToInt32(ReadLine());
    
        index = Array.FindIndex(phoneAreacode, w => w == areaCode);
        if (index >= 0)
        {
            validAreacode = true;
        }
        else
        {
            WriteLine("YOU MUST ENTER A VALID AREA CODE!");
        }
    
    } while (!validAreacode);
    
    
    Write("Enter in the length of your call: ");
    callLength = Convert.ToInt32(ReadLine());
    double finalCost = callLength * phoneCost[index];
    WriteLine("Your call to area code " + areaCode + " for " + callLength + " minutes will cost " + finalCost.ToString("C"));
    

    如您所见,您还可以删除 while 您必须循环数组和 if-else 以获得有效代码。假设当代码到达该点时,该区域是正确的。

    最好尝试删除if-else 的号码。

    【讨论】:

      【解决方案3】:

      你可能需要循环整个代码部分,像这样

      while (true)
      {
      
        Write("Enter in the area code you want to call: ");
      
        var input = ReadLine();
      
        if (input == "Exit")
            break;
      
        areaCode = Convert.ToInt32(input);
        x = 0;
      
      while (x < phoneAreacode.Length && areaCode != phoneAreacode[x])
             ++x;      
      
        if(x != phoneAreacode.Length)
        {
             validAreacode = true;
              cost = phoneCost[x];
         }
        else 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!");
         }
      
      }
      

      不要忘记在用户输入非数字内容时添加检查

      【讨论】:

        猜你喜欢
        • 2014-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-19
        • 2020-09-19
        相关资源
        最近更新 更多