为输入到 int 变量创建一个整数范围

问题描述

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

整个代码,只有代码(和注释)

                     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 个空格和 * 在正确的空格中,我真的没有任何线索,哈哈。

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

谢谢

克里斯

解决方法

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...