【问题标题】:Creating an Integer range for input into int variable为 int 变量的输入创建一个整数范围
【发布时间】:2021-06-04 10:55:52
【问题描述】:

所以我有一个项目,我需要存储 8 个整数,介于 1-10 之间,并将它们召回到直方图中 直方图仅由字符 * 组成。 (原谅格式不好,这是我的第一篇文章) 该程序有效,但我认为我的 int 限制 (1-10) 的 if 语句可以简化。 我的目标是用尽可能少的代码实现这个输出

整个代码,只有代码(和 cmets)

                     Console.WriteLine("Hello and Welcome!\nEnter 8 values between 1 and 10\n*Press Any Key to continue*");
                     Console.ReadKey();

            //create list to store values
            List<int> values = new List<int>();

            //loop to collect values
            for (int i = 1; i < 9; i++)
            {
                //label for data validation start point
                 retry:
                        Console.WriteLine("Please Enter Value " + i, "Between 1 - 10");
                //variable assigned to user input
                value = Console.ReadLine();

                //Convert string to integer
                if (!int.TryParse(value, out validValue)) 
                { 
                        Console.WriteLine("~Incorrect Data Input~"); goto retry; };
                if (validValue < 1) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };
                if (validValue > 10) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };
            
                 values.Add(validValue); 
            }
                 
            for (int i = 0; i < 8; i++ )
            {
                Console.WriteLine();
                for(int id = 0; id < values[i]; id++)
               
                         Console.Write("*");
            }
            
                         Console.ReadKey();

这是我认为可以更清洁的区域

if (validValue < 1) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };
                if (validValue > 10) { Console.WriteLine("~Incorrect Data Input~"); goto retry; };

我愿意接受有关如何清理这个或项目的任何部分的任何建议。

这个项目有一个更困难的方面,比如如果你想炫耀某种东西,我需要垂直显示直方图。 我想这可以用二维数组来完成吗?除了在正确的空格中需要 80 个空格和 * 之外,我真的没有任何线索。哈哈。

我只写了一个月左右的代码,它是基于学习者的,欢迎任何帮助或建议

谢谢

克里斯

【问题讨论】:

  • 到目前为止,你做得很好。继续以您可以弄清楚如何回答的方式提出问题。一些建议... 如果可能的话,永远不要使用goto。尝试定义变量int validValue = 0;,然后使用while 循环继续循环,直到用户定义它while (validValue == 0) { //ask user to define it }
  • 为什么?没有转到??谢谢你的客气话

标签: c# list validation variable-assignment restrict


【解决方案1】:

这里有一个示例,说明如何使用几个指针重构逻辑。

Console.WriteLine("Hello and Welcome!\nEnter 8 values between 1 and 10\n*Press Any Key to continue*");
Console.ReadKey();

//create list to store values
List<int> values = new List<int>();

//loop to collect values (no need to run from 1 - 9; 0 - 8 works fine.)
for (int i = 0; i < 8; i++)
{
    //label for data validation start point
    int validValue = 0;

    //Use a while loop instead of a goto
    while (validValue == 0)
    {
        Console.WriteLine("Please Enter Value " + i, "Between 1 - 10");
        //variable assigned to user input
        var value = Console.ReadLine();

        //Convert string to integer
        //If the first question in the predicate fails, it won't go on to ask the other questions
        if (int.TryParse(value, out validValue) && validValue > 0 && validValue < 10)
        {
            values.Add(validValue);
        }
        else
        {
            Console.WriteLine("~Incorrect Data Input~");
        }
        //If the code reaches this point and validValue hasn't been changed, it'll be 0 and the loop will run again
    }
}

for (int i = 0; i < 8; i++)
{
    Console.WriteLine();
    for (int id = 0; id < values[i]; id++)

        Console.Write("*");
}

Console.ReadKey();

【讨论】:

  • 是的,内特,谢谢你,伙计。这就是我要找的&&。那么为什么要使用 while 循环而不是 for 呢?
  • 这在第一个 writeline 输出处循环
  • 据我了解您的程序的目的,这与您发布的内容完全相同,但将 goto 替换为 while 循环。
  • 只是为了澄清你问的第一个问题,这里有两种循环。 for 循环正在处理您尝试收集的 8 个值的增量,并且在该循环的每次迭代中,while 循环确保每次传递都收集可用、已解析的有效数据。在每个for 循环循环中,while 将根据需要循环多次,直到用户输入有效数据。
  • 是的,我对这里使用的循环有基本的了解。我尝试了您提供的代码,并且程序不会因用户输入而停止。它一遍又一遍地请求一个值。我不明白它为什么这样做,它不等待用户输入是没有意义的
猜你喜欢
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多