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

问题描述 投票:0回答:7

我目前正在寻找此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”,但是还有其他方法可以实现代码的输出,以便当用户不输入相应的选择时,再次循环?

c#
7个回答
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;
        }
    }

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
投票

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

        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.

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());
}

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]();

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; }
    }
}

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子句。但是,让它成为另一天的讲座。 :)

您可以进行进一步的改进。在所有情况下,您都叫Console.WriteLine(""),所以将其移到外面。

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

代替固定的字符串,您可以连接P1Choice的值。

while (true) {
    int P1Choice = int.Parse(Console.ReadLine());
    Console.WriteLine("");
    if (1 <= P1Choice && P1Choice <= 4) {
        CenterWrite("You have chosen Default Empire " + P1Choice);
    } else {
        CenterWrite("Input Invalid, Please press the number from the corresponding choices to try again");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.