如何在控制台应用程序中创建可终止的while循环?

时间:2019-01-02 12:15:40

标签: c#

我目前正在寻找此c#控制台应用程序功能的解决方案

我试图寻找一种创建while循环的方法,该方法可以终止下面的代码,但是我只想出了与while循环中断有关的结果,或者是不将其放入while循环的解决方案


        int P1Choice = int.Parse(Console.ReadLine());

        while (true)
        {
            if (P1Choice == 1)
            {
                Console.WriteLine("");
                CenterWrite("You have chosen Defult Empire 1");
                break;
            }
            if (P1Choice == 2)
            {
                Console.WriteLine("");
                CenterWrite("You have chosen Defult Empire 2");
                break;
            }
            if (P1Choice == 3)
            {
                Console.WriteLine("");
                CenterWrite("You have chosen Defult Empire 3");
                break;
            }
            if (P1Choice == 4)
            {
                Console.WriteLine("");
                CenterWrite("You have chosen Defult Empire 4");
                break;
            }
            else
            {
                Console.WriteLine("");
                CenterWrite("Input Invalid, Please press the number from the corresponding choices to try again");
                Console.ReadKey();
                int P1Choice = int.Parse(Console.ReadLine());
            }
        }

我了解到我无法在此范围内声明局部参数“ P1Choice”,但是还有其他方法可以实现代码的输出,以便在用户不输入相应选择时,它又循环了吗?

7 个答案:

答案 0 :(得分:3)

如果仅当满足某些语句时才想退出while循环,那么这就是进入循环时应声明的内容。

我将使用boolean来了解用户是否做出了正确的选择。

bool right_choice = false;
int P1Choice = int.Parse(Console.ReadLine());
while(!right_choice) {
    switch(P1Choice) {
        case 1: 
             right_choice = true;
             {case 1 code};
             break;
        case 2:
             right_choice = true;
             {case 2 code};
             break;
        case 3:
             right_choice = true;
             {case 3 code};
             break;
        case 4:
             right_choice = true;
             {case 4 code};
             break;
         default:
             break;
    }
    if (!right_choice) {
        Console.WriteLine("");
        CenterWrite("Input Invalid, Please press the number from the corresponding choices to try again");
        Console.ReadKey();
        P1Choice = int.Parse(Console.ReadLine());
    }    
  }    
}

这样,一旦用户做出正确选择,您就退出循环。 请注意,我将您的代码更改为使用switch case而不是4个if,因为这是实现用户输入选择的公认方法。

祝你好运!

答案 1 :(得分:1)

您只需要在while循环中使用readline,否则在其他情况下也会中断。它应该这样工作:

 int P1Choice;

    while (true)
    {
    P1Choice = int.Parse(Console.ReadLine());

        if (P1Choice == 1)
        {
            Console.WriteLine("");
            CenterWrite("You have chosen Defult Empire 1");
            break;
        }
        if (P1Choice == 2)
        {
            Console.WriteLine("");
            CenterWrite("You have chosen Defult Empire 2");
            break;
        }
        if (P1Choice == 3)
        {
            Console.WriteLine("");
            CenterWrite("You have chosen Defult Empire 3");
            break;
        }
        if (P1Choice == 4)
        {
            Console.WriteLine("");
            CenterWrite("You have chosen Defult Empire 4");
            break;
        }
        else
        {
            Console.WriteLine("");
            CenterWrite("Input Invalid, Please press the number from the corresponding choices to try again");
    break;
        }
    }

答案 2 :(得分:1)

我希望这是您所需要的。可能值在列表“列表”中,并且一直循环直到答案是可能值之一:

        int value = 0;

        List<int> list = new List<int> { 1, 2, 3, 4 }; // choices are in the list

        while (true)
        {
            Console.WriteLine("Please enter a number :");
            if (int.TryParse(Console.ReadLine(), out value))
            {
                if (list.Contains(value))
                    break;
            }
        }

        // value is in the list, deal with it.

答案 3 :(得分:0)

一种选择是创建方法并继续调用,直到输入有效:

public static void ProcessInput(string input)
{
    int choice = Convert.ToInt32(input);
    switch (choice)
    {
        case 1:
            Console.WriteLine("");
            Console.WriteLine("You have chosen Defult Empire 1");
            break;
        case 2:
            Console.WriteLine("");
            Console.WriteLine("You have chosen Defult Empire 2");
            break;
        case 3:
            Console.WriteLine("");
            Console.WriteLine("You have chosen Defult Empire 3");
            break;
        case 4:
            Console.WriteLine("");
            Console.WriteLine("You have chosen Defult Empire 4");
            break;
        default:
            Console.WriteLine("");
            Console.WriteLine("Input Invalid, Please press the number from the corresponding choices to try again");
            ProcessInput(Console.ReadLine());
            break;
    }

并在您的主程序中:

public static void Main()
{
    Console.WriteLine("Hello World");
    ProcessInput(Console.ReadKey());
}

答案 4 :(得分:0)

您有2个问题: 1.您的代码无法编译,因为您尝试两次绑定P1Choice。 2.您在else情况下要求输入两次。

要修复1.,必须从第二次出现的int(在P1Choice情况下删除)中删除else

要修复2.,必须在Console.readKey()情况下删除else

此外,如果您使用else if子句而不是仅使用if子句,您的代码将更易于阅读。

while (true) {
    int P1Choice = int.Parse(Console.ReadLine());
    if (P1Choice == 1) {
        Console.WriteLine("");
        CenterWrite("You have chosen Default Empire 1");
    } else if (P1Choice == 2) {
        Console.WriteLine("");
        CenterWrite("You have chosen Default Empire 2");
    } else if (P1Choice == 3) {
        Console.WriteLine("");
        CenterWrite("You have chosen Default Empire 3");
    } else if (P1Choice == 4) {
        Console.WriteLine("");
        CenterWrite("You have chosen Default Empire 4");
    } else {
        Console.WriteLine("");
        CenterWrite("Input Invalid, Please press the number from the corresponding choices to try again");
    }
}

此外,我建议您使用switch子句,而不要使用许多if子句。但是,让它成为另一天的讲座。 :)

答案 5 :(得分:0)

您可以执行以下操作

var options = new Dictionary<int, Action> {
{1, () => {
    //opt 1 code
}},
{2, () => {
    //opt 2 code
}},
{3, () => {
    //opt 3 code
}},
{4, () => {
    //opt 4 code
}}
};

Console.WriteLine("Please enter you choice:");

int P1Choice;
while (!(int.TryParse(Console.ReadLine(), out P1Choice) && options.ContainsKey(P1Choice)))
{
    Console.WriteLine("Input Invalid, Please press the number from the corresponding choices to try again:");
}

options[P1Choice]();

答案 6 :(得分:0)

这是基于您删除的帖子的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BattleGrid grid = new BattleGrid();
            grid.PrintGrid();
            Console.ReadLine();
        }
    }
    public class BattleGrid
    {
        public List<List<BattleGridCell>> grid = new List<List<BattleGridCell>>();

        public BattleGrid()
        {
            for (int row = 0; row < 4; row++)
            {
                List<BattleGridCell> newRow = new List<BattleGridCell>();
                grid.Add(newRow);
                for (int col = 0; col < 4; col++)
                {
                    BattleGridCell newCell = new BattleGridCell();
                    newRow.Add(newCell);
                    newCell.rowLetter = ((char)((int)'A' + row)).ToString();
                    newCell.colnumber = col.ToString();
                }
            }
        }
        public void PrintGrid()
        {
            foreach (List<BattleGridCell> row in grid)
            {
                Console.WriteLine("|" + string.Join("|", row.Select(x => "X" + x.rowLetter + x.colnumber))); 
            }
        }
    }

    public class BattleGridCell
    {
        public string rowLetter { get; set; }
        public string colnumber { get; set; }
    }
}