Why is my array only accepting the last number taken as the first element, and putting 0 as the rest of the elements?

huangapple go评论56阅读模式
英文:

Why is my array only accepting the last number taken as the first element, and putting 0 as the rest of the elements?

问题

验证部分根据我测试的结果运行正常(测试了所有可能触发各种解析检查的情况),我遇到的困难是在经过验证后将用户输入输入为不是字符且在0到100之间作为双精度浮点数。

数组将最后一个输入作为第一个元素,并将其余元素设置为0。

我尝试将它移动到第二个主要if的末尾,即在括号外但在最后一个if语句的失败情况之前,但结果相同,除此之外,我不知道还能改变什么。

注意:数组的大小从主函数内获取。

以下是上述代码的翻译:

private static void InputArray(params double[] number)
{
    double d1;
    int i1 = 0;
    int k = 0;
    if (k < number.Length)
    {
        Console.Write("输入一个数字:");
    Reset:
        string input = Console.ReadLine();
        if (char.TryParse(input, out char c1))
        {
            if (c1 < -1 || c1 > 11)
            {
                Console.WriteLine("输入的值:" + c1 + " 不是有效的输入");
                goto Reset;
            }
        }
        if (double.TryParse(input, out d1) || int.TryParse(input, out i1))
        {
            if ((d1 < 0.0 || d1 > 100.0) || (i1 < 0 || i1 > 100))
            {
                Console.WriteLine("输入超出有效输入范围。");
                goto Reset;
            }
            else
                goto end;
        }
        else if (!double.TryParse(input, out d1))
        {
            Console.WriteLine("输入结束成功。");
        }
    }
    end:
    for (int e = 0; e < number.Length;)
    {
        number[e] = double.Parse(input);
        e++;
    }
}

这是从主函数调用上述方法的方式:

for (int i = 0; i < number.Length; i++)
{
    InputArray(number);
}
英文:

The validation works as far as i had tested (that being all possible cases that could trigger the various parse checks), what I`m struggling with is inputing user input after its been validated to not be a character and within 0 - 100 as a double.
The array takes the last input as the first element and sets the rest to 0.

I tried moving it and the end of the second major if, so outside the brackets but before the fail case at the last if statment. and the same result occured, beyond this I dont know what more I could change.

Note: the size of the array is gotten from within main.

private static void InputArray (params double [] number)
        {
            double d1;
            int i1 = 0;
            int k = 0;
            if (k &lt; number.Length)
            {
                Write (&quot;Enter a number &quot;);
            Reset:
                input = ReadLine ();
                if (char.TryParse (input, out char c1))
                {
                    if ((c1 &lt; -1) &amp;&amp; (c1 &gt; 11))
                    {
                        WriteLine (&quot;The input: &quot; + c1 + &quot; is not an accepted input&quot;);
                        goto Reset;
                    }
                }
                if ((double.TryParse (input, out d1)) || (int.TryParse (input, out i1)))
                {
                    if (((d1 &lt; 0.0) || (d1 &gt; 100.0)) || ((i1 &lt; 0) || (i1 &gt; 100)))
                    {
                        WriteLine (&quot;Input is outside of the range of valid inputs.&quot;);
                        goto Reset;
                    }
                    else
                        goto end;
                }
                else if (!double.TryParse (input, out d1))
                {
                    WriteLine (&quot;End of input successful.&quot;);
                }
            }
            end:
            for(int e = 0; e &lt; number.Length;)
            {
                number [e] = double.Parse (input);
                e++;
            }
        }

Heres the method call from main for the above method

for (int i = 0; i &lt; number.Length; i++)
            {
                InputArray (number);

            }

答案1

得分: 0

以下是翻译的内容:

你是否只是尝试用用户输入的数字(在0.0100.0之间,包括0.0100.0)填充整个数组?

static String input;

static void Main(string[] args)
{
    double[] number = new double[10];
    InputArray(number);

    WriteLine("数组内容:");
    WriteLine(String.Join(", ", number));

    Write("按任意键退出...");
    ReadKey();
}

private static void InputArray(double[] number)
{
    double d1;
    for (int k = 0; k < number.Length; k++)
    {
        bool valid = false;
        do
        {
            Write("输入一个在0.0和100.0之间的数字:");
            input = ReadLine();
            valid = double.TryParse(input, out d1) && (d1 >= 0.0) && (d1 <= 100.0);
            if (!valid)
            {
                WriteLine("输入无效或超出接受范围。");
            }
        } while (!valid);
        number[k] = d1;
    }
}

示例运行:

输入一个在0.0100.0之间的数字:tacos are great
输入无效或超出接受范围。
输入一个在0.0100.0之间的数字:-4
输入无效或超出接受范围。
输入一个在0.0100.0之间的数字:100.1
输入无效或超出接受范围。
输入一个在0.0100.0之间的数字:0
输入一个在0.0100.0之间的数字:0.0
输入一个在0.0100.0之间的数字:100
输入一个在0.0100.0之间的数字:100.0
输入一个在0.0100.0之间的数字:3.1415926
输入一个在0.0100.0之间的数字:50
输入一个在0.0100.0之间的数字:72.3
输入一个在0.0100.0之间的数字:unicorns
输入无效或超出接受范围。
输入一个在0.0100.0之间的数字:83.4
输入一个在0.0100.0之间的数字:92
输入一个在0.0100.0之间的数字:1.3
数组内容:
0, 0, 100, 100, 3.1415926, 50, 72.3, 83.4, 92, 1.3
按任意键退出...
英文:

Are you simply trying to fill the entire array with numbers entered by the user between 0.0 and 100.0 inclusive?

static String input;
static void Main(string[] args)
{
double[] number = new double[10];
InputArray(number);
WriteLine(&quot;Array contents:&quot;);
WriteLine(String.Join(&quot;, &quot;, number));
Write(&quot;Press any key to quit...&quot;);
ReadKey();
}
private static void InputArray(double[] number)
{
double d1;
for(int k=0; k&lt;number.Length; k++)
{
bool valid = false;
do
{
Write(&quot;Enter a number between 0.0 and 100.0: &quot;);
input = ReadLine();
valid = double.TryParse(input, out d1) &amp;&amp; (d1 &gt;= 0.0) &amp;&amp; (d1 &lt;= 100.0);
if (!valid)
{
WriteLine(&quot;Input is invalid or outside the accepeted range.&quot;);
}
} while (!valid);
number[k] = d1;
}
}

Sample run:

Enter a number between 0.0 and 100.0: tacos are great
Input is invalid or outside the accepeted range.
Enter a number between 0.0 and 100.0: -4
Input is invalid or outside the accepeted range.
Enter a number between 0.0 and 100.0: 100.1
Input is invalid or outside the accepeted range.
Enter a number between 0.0 and 100.0: 0
Enter a number between 0.0 and 100.0: 0.0
Enter a number between 0.0 and 100.0: 100
Enter a number between 0.0 and 100.0: 100.0
Enter a number between 0.0 and 100.0: 3.1415926
Enter a number between 0.0 and 100.0: 50
Enter a number between 0.0 and 100.0: 72.3
Enter a number between 0.0 and 100.0: unicorns
Input is invalid or outside the accepeted range.
Enter a number between 0.0 and 100.0: 83.4
Enter a number between 0.0 and 100.0: 92
Enter a number between 0.0 and 100.0: 1.3
Array contents:
0, 0, 100, 100, 3.1415926, 50, 72.3, 83.4, 92, 1.3
Press any key to quit...

huangapple
  • 本文由 发表于 2023年2月24日 00:50:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定