尝试循环回到控制台应用程序的开头

时间:2019-01-09 11:51:52

标签: c# console

我正在评估板上,我们必须输出一幅画是纵向还是横向。我无法计算出放置位置,或者即使用户输入了无效的字符串,循环也无法在我的代码中重新发送。我的代码看起来像这样。

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("What is the Height of the Painting?");

        string input = Console.ReadLine();
        int height;

        if (!int.TryParse(input, out height))
        {
            Console.WriteLine("Invalid number. Please make it an integer... e.g 1-9");
        }
        else
        {
            Console.WriteLine("What is the Width of the Painting?");

            string input2 = Console.ReadLine();
            int width;

            if (!int.TryParse(input2, out width))
            {
                Console.WriteLine("Invalid number. Please make it an integer... e.g 1-9");
            }
            else
            {
                var orientation = (height > width ? Orientation.Landscape : Orientation.Portrait);

                Console.WriteLine("Your Painting is currently in the: " + orientation + " orientation");
            }
        }
    }

    public enum Orientation
    {
        Landscape,
        Portrait
    }
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您将想要重复的所有内容都圈了起来。记住要提供一种突破的方法:

class Program
{
    static void Main(string[] args)
    {

      while(true) { //run forever, we'll break out if the user wants to quit

        Console.WriteLine("What is the Height of the Painting?");

        string input = Console.ReadLine();
        int height;


        if ("int.TryParse(input, out height))
        {
            Console.WriteLine("Invalid number. Please make it an integer... e.g 1-9");
        }
        else
        {
            Console.WriteLine("What is the Width of the Painting?");
            string input2 = Console.ReadLine();
            int width;

            if (!int.TryParse(input2, out width))
            {
                Console.WriteLine("Invalid number. Please make it an integer... e.g 1-9");
            }
            else
            {
                var orientation = (height > width ? Orientation.Landscape : Orientation.Portrait);
                Console.WriteLine("Your Painting is currently in the: " + orientation + " orientation");
            }
        }


        Console.WriteLine("Do another? Enter yes to do another");

        string input = Console.ReadLine();
        if(input != "yes")
        {
          break; //exit loop
        }

      } //end of while loop
    }

    public enum Orientation
        {
            Landscape,
            Portrait

        }
    }
}

如果您已经学会了编写自己的方法,可以考虑使用它们来减少代码重复:

使用系统;

class Program
{
    static void Main(string[] args)
    {

        int height = AskForInt("What is the Height of the Painting?");
        int width = AskForInt("What is the Width of the Painting?");

        var orientation = (height > width ? Orientation.Landscape : Orientation.Portrait);

        Console.WriteLine("Your Painting is currently in the: " + orientation + " orientation");

    }

    static int AskForInt(string question) {
        Console.WriteLine(question);

        while (true) //use a loop to keep asking the user if they didn't provide a valid answer
        {
            string input = Console.ReadLine();
            int answer;

            if (!int.TryParse(input, out answer))
            {
                Console.WriteLine("Not a valid integer. Please enter an integer: ");
            }
            else
            {
                return answer; //exit this method, returning the int
            }
        }
    }

    public enum Orientation
    {
        Landscape,
        Portrait

    }
}

我将使后面的示例成为永远循环的事情,作为读者的练习:)

答案 1 :(得分:1)

不要重复自己提取方法

static int ReadInt(string title) {
  int result = 0;

  while (true) {
    Console.WriteLine(title);

    if (int.TryParse(Console.ReadLine(), out result)) 
      return result;

    Console.WriteLine("Invalid number. Please make it an integer... e.g 1-9");
  }
}

然后使用它:如果要一次计算

static void Main(string[] args) {
  int height = ReadInt("What is the Height of the Painting?");
  int width = ReadInt("What is the Width of the Painting?");

  Console.WriteLine(
    $"Your Painting is currently in the: {((height > width ? "portrait" : "landscape")} orientation");
}

如果要多次计算:

static void Main(string[] args) {
  while (true) {
    int height = ReadInt("What is the Height of the Painting?");
    int width = ReadInt("What is the Width of the Painting?");

    Console.WriteLine(
      $"Your Painting is currently in the: {((height > width ? "portrait" : "landscape")} orientation");

    Console.WriteLine("Would you like to compute another sizes (y/n)?");

    if (string.Equals("n", Console.ReadLine().Trim(), StringComparer.OrdinalIgnoreCase))
      break;
  }
}