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