C# 代码错误分析及修正: 循环索引和数组赋值
这段代码存在一些错误:
- 在循环中,应该使用
i来索引数组a和b,而不是N。 - 在循环中,应该将
b[i]赋值为int.Parse(a[i]),而不是b[i] = int.Parse(a[i])。 - 在循环结束后,需要使用
i来索引数组str,而不是固定的索引值。 - 最后一行的
Console.ReadKey()可以省略。
修正后的代码如下:
using System;
namespace t1_3_evolution
{
internal class Program
{
static void Main(string[] args)
{
int N = int.Parse(Console.ReadLine());
string[] str = new string[N];
for (int i = 0; i < N; i++)
{
string number = Console.ReadLine();
string[] a = number.Split(' ');
int[] b = new int[3];
for (int j = 0; j < 3; j++)
{
b[j] = int.Parse(a[j]);
}
if (b[0] * b[1] == b[2])
{
str[i] = 'LvYan';
}
else if (b[0] + b[1] == b[2])
{
str[i] = 'TuDou';
}
else
{
str[i] = 'who?';
}
}
for (int i = 0; i < N; i++)
{
Console.WriteLine(str[i]);
}
}
}
}
原文地址: https://www.cveoy.top/t/topic/pkrV 著作权归作者所有。请勿转载和采集!