【发布时间】:2021-03-14 14:23:42
【问题描述】:
我是 C# 新手,我正在尝试制作一个程序,提示用户输入有关 8 位歌手的信息,然后根据 vocalist.genre 输入的内容将这些信息分类到文本文件中。但是,当我尝试将 for 循环用于用户的输入时,我遇到了问题。我该怎么办?
using System;
using System.IO;
struct vocalists
{
public string name;
public string origin;
public string vocalist_type;
public string genre;
};
public class test
{
public static void Main(string[] args)
{
vocalists vocalists1;
vocalists vocalists2;
vocalists vocalists3;
vocalists vocalists4;
vocalists vocalists5;
vocalists vocalists6;
vocalists vocalists7;
vocalists vocalists8;
//Vocalists inputs
for (int i = 1; i < 9; i++)
{
Console.WriteLine("Enter the vocalists {0} name: ", i);
//Here I'm supposed to do something in the format of vocalistsi.name=Console.ReadLine();
Console.WriteLine("Enter the vocalists {0} origin: ", i);
//Here I'm supposed to do something in the format of vocalistsi.origin=Console.ReadLine();
Console.WriteLine("Enter the vocalists {0} type: ", i);
//Here I'm supposed to do something in the format of vocalistsi.type=Console.ReadLine();
Console.WriteLine("Enter the vocalists {0} genre: ", i);
//Here I'm supposed to do something in the format of vocalistsi.genre=Console.ReadLine();
}
}
}
【问题讨论】:
-
你期待什么行为?
-
使用数组代替 8 个变量
-
像@juharr 我会建议使用数组或列表,而不是像你那样做。但如果您坚持使用变量名,您可以在 Google 上搜索“C# 中的反射”。
标签: c# loops for-loop structure