C#用户输入设置数组大小并循环数组以显示元素

3
目标是通过测试分数数量从用户输入中创建数组大小。然后创建一个循环,通过提示用户输入0到100的每个测试分数来填充数组。最后使用另一个循环显示结果。
问题:为什么当输入测试分数(例如“50”)时,它会将50个元素添加到数组中?
任何帮助都将不胜感激,谢谢。我看到了一些类似的帖子,但无法解决这个问题。此外,其中一个是用西班牙语写的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {


                // prompt user to ask how many test scores to build the size of the array


                Write("How many test scores total: ");
                string sSize = ReadLine();          
                int i = Convert.ToInt32(sSize);              
                int[] score = new int[i];



            // create the loop of asking the test scores limited to the array sSize

            for (int a = 1; a < i + 1; a++)
            {

                Write("Please enter a test score " + a + " from 0 to 100: ");
                string testArray = ReadLine();


                int g = Convert.ToInt32(testArray);

                int[] tests = new int[g];

                //create loop to display all test scores
                foreach (var item in tests)
                    Console.WriteLine(item);





                }

            }
        }
}

int [] score = new int [i]; 你在哪里设置这些分数的代码?当你实例化数组时,它将自动填充为0。但是你没有任何改变这些0为其他值的代码。 - mjwills
@mjwills和umer-非常感谢你们两个,我明白了自己犯的错误。 - Elements
3个回答

2

int[] tests = new int[g];

在这里,您正在为数组分配用户提供的大小,而不是填充数组,您缺少填充语句或查询。

请注意保留html标签。


好的,这很明白,我看错了,应该是设置大小而不是填充。谢谢! - Elements

1
因为你在循环内部创建了一个新的数组,其大小与用户输入的“分数”相同,然后你遍历它。这些值都是零,因为当你创建一个数组时,它会用类型的默认值填充,本例中为0。第二个循环应该在第一个循环之后,并且你不应该在第一个循环内部创建数组,只需填充最初创建的原始数组(score)即可。
以下是你实际想要的内容。请注意,你应该从0开始索引而不是1。
Write("How many test scores total: ");
string sSize = ReadLine();          
int i = Convert.ToInt32(sSize);              
int[] score = new int[i];

// create the loop of asking the test scores limited to the array sSize
for (int a = 0; a < i; a++)
{
    Write("Please enter a test score " + (a + 1) + " from 0 to 100: ");
    string testArray = ReadLine();
    int g = Convert.ToInt32(testArray);
    score[a] = g;
}

//create loop to display all test scores
foreach (var item in score)
    Console.WriteLine(item);

您可能还想考虑使用int.TryParse,这样您就可以确定用户是否输入了无效值。


非常感谢对代码的排序!我最初从索引0开始,但在另一个类似的程序中它没有正常工作,所以使用了这种方法。我同意这似乎不正确或有效率。谢谢! - Elements

0

我认为你从不同的地方复制了代码片段,但是没有能够正确地将它们组合在一起。

你在这里创建了一个不必要的数组:int[] tests = new int[g];。然后试图使用它只会让你的情况变得更糟。

此外,你没有正确处理索引。当你学习编程时,使用适当的格式和良好的变量名将有助于你更好地理解自己的代码(可能来自不同的地方),从而提高你的“调试”技能。

我有一个“修复”的版本的代码,应该是不言自明的。

using System;
using static System.Console;

namespace ConsoleApp3 {
    class Program {
        static void Main(string[] args) {
            // prompt user to ask how many test scores to build the size of the array
            Write("How many test scores total: ");
            string testCountStr = ReadLine();
            int testCount = Convert.ToInt32(testCountStr );
            int[] testScores = new int[testCount];

            // create the loop of asking the test scores limited to the array sSize
            for (int i = 0; i < testCount; i++) {
                Write($"Please enter score for test {i + 1} from 0 to 100: ");
                string scoreStr = ReadLine();
                int score = Convert.ToInt32(scoreStr);
                if (score > 100 || score < 0) {
                    //TODO: handle your error
                    WriteLine("Invalid score. Please try again");
                    --i;
                    continue;
                }
                testScores[i] = score;
            }

            WriteLine();
            // create loop to display all test scores
            for (int i = 0; i < testScores.Length; ++i)
                WriteLine($"Your score for test {i + 1} is {testScores[i]}");


        }
    }
}

你是正确的!情景是这样的:我通常会提前看一章,了解接下来的内容,然后在脑海中或纸上规划程序。有一次我无意中看错了章节,开始了一个更具挑战性的多维数组程序。这非常难以理解,需要大量的编码工作。后来我发现自己已经超前了,认为可以修改当前的代码,因为它们很相似...结果我错了。感谢您的建议! - Elements

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接