【发布时间】:2021-06-26 00:53:32
【问题描述】:
首先,这是我的第一个问题,很抱歉没有遵循指导方针,我的错,下次会做得更好。
简而言之,我有一个 CSV 文件中的乐透值,我可以读取该文件并将它们放入一个数组中。一旦进入数组,我必须对每一行(球)应用一些统计公式,所以我基本上创建另一个数组来保存这些值。不过好像我说的还不够高。
到目前为止,我得到了一些 cmets,但并不是真正让我烦恼的地方。它与我如何使“tirages”全局化以便我可以在整个代码中访问其内容有关?
如果您查看代码,您会在第 40 行左右看到这个 ...
int derniertirageacompter = tirages.Count;
Console.WriteLine($"dernier tirage: {derniertirageacompter}")
... 它返回 0。但我在“Load_649_CSV”中的其他地方引用了 tirages,它工作正常。我尝试了视觉工作室提出的解决方案,但没有奏效,所以我在这里。
我认为问题在于“tirages”以及如何/在何处宣布它,但我不知道。显然,我不是程序员! 20 年前我在 Excel VBA 中编写了一个程序,我正在尝试用 c# 重写它。我将不得不使用大量的数组来重新创建我使用的存储公式和数据的电子表格。这就是我尝试在全球范围内访问 tirages 的原因。
...
//Main Program
//
namespace Lotto_649
{
class Program
{
//public static object Frequence { get; private set; }
static void Main(string[] args)
{
int Nb_Boule;//49 for 649 and 50 for Lotto_MAX
MenuPrincipale();//display the main menu
//load the draw based on the choice
int Menu_Lotto_Choice = Convert.ToInt16(Console.ReadLine());
switch (Menu_Lotto_Choice)//faire une selection
{
case 1:
Console.WriteLine("Loading 649");
Load_649_CSV();//call method for loading the 649 drwas
Nb_Boule = 49;
break;
case 2:
Console.WriteLine("To Do Lotto MAX");
//Load_Lotto_max();//call method for the lotto max draw
Nb_Boule = 50;
break;
default:
Console.WriteLine("Error - Break");//any other keys
break;
}//end switch
//this is where I have I think an issue!
var tirages = new List<Tirage>();
int derniertirageacompter = tirages.Count;
Console.WriteLine($"dernier tirage: {derniertirageacompter}");
}
//---------------------------Declaring the method used in the main program
//Load the 649 draw in the array.
public static void Load_649_CSV()
{
//read the content of the CSV file as plain text
//read all the lines and put it directly into an array
//Date,Boule 1,Boule 2,Boule 3,Boule 4,Boule 5,Boule 6,Complementaire,Valeur,Even - Odd,Buckets
string[] csvAllTirage649 = System.IO.File.ReadAllLines(@"C:\Users\repos\CSV-Reader-with-lotto\Tirage649.csv");
int Num_of_Draw_to_Check;//variable that determine how many draw to check
//**Declaring the array tirages**
//How to declare the array so its available globally? is this the way to do it?
var tirages = new List<Tirage>();
Console.WriteLine($"There are {csvAllTirage649.Length} in the file.");
Console.WriteLine($"How many resutls to load?");
string v = Console.ReadLine();
//**add error catcher
Num_of_Draw_to_Check = Convert.ToInt16(v);
for (int i = 1; i < Num_of_Draw_to_Check; i++)//start with 1 since 0 content the header
{
Tirage draw = new Tirage(csvAllTirage649[i]);//creat the array drawy abd laod the results into it.
tirages.Add(draw);
}
Console.WriteLine($"The {Num_of_Draw_to_Check} sesutls are loaded");
}// End Load_649_CSV
//----------------------Print to screen the menu---------------------------------
private static void MenuPrincipale()
{
//menu principale
Console.WriteLine($"-------------------------------");
Console.WriteLine($"Beginning of the program");
Console.WriteLine($"Select the lotto type");
Console.WriteLine($"1 - 649");
Console.WriteLine($"2 - Lotto Max");
Console.WriteLine($"-------------------------------");
//end menuprincipale
}
}
}
...
这是我的课 ...
namespace Lotto_649
{
//on defini la classe tirage qui est utiliser pour lire le fichier CSV
public class Tirage
{
public DateTime t_Date;
public int B1, B2, B3, B4, B5, B6, Comp, Somme_Tir_wo_Compl, Pair;
//should I use and array to store the boule value?
//public int[] Boules = new int[6];
public string Montant, Even_Odd, Bucket;
public Tirage(string rowTirage649)
{
// Date,Boule 1,Boule 2,Boule 3,Boule 4,Boule 5,Boule 6,Complementaire,Valeur,Even - Odd,Buckets
string[] data = rowTirage649.Split(',');
//parse data into properties
//convert from a string to a date
this.t_Date = Convert.ToDateTime(data[0]);
//convert from a string to int
this.B1 = Convert.ToInt16(data[1]);
this.B2 = Convert.ToInt16(data[2]);
this.B3 = Convert.ToInt16(data[3]);
this.B4 = Convert.ToInt16(data[4]);
this.B5 = Convert.ToInt16(data[5]);
this.B6 = Convert.ToInt16(data[6]);
this.Comp = Convert.ToInt16(data[7]);
this.Montant = data[8];
this.Even_Odd = data[9];
this.Bucket = data[10];
this.Somme_Tir_wo_Compl = B1 + B2 + B3 + B4 + B5 + B6;
//init Pair, then count the nombre de boule pair.
Pair = 0;
{
if (B1 % 2 == 0)
Pair++;
if (B2 % 2 == 0)
Pair++;
if (B3 % 2 == 0)
Pair++;
if (B4 % 2 == 0)
Pair++;
if (B5 % 2 == 0)
Pair++;
if (B6 % 2 == 0)
Pair++;
}
}
public override string ToString()
{
string str = $"{t_Date} {B1} {B2} {B3} {B4} {B5} {B6} {Comp} {Montant} {Even_Odd} {Pair}:";
return str;
}
}
}
...
【问题讨论】:
-
请正确格式化您的代码。使用鼠标突出显示代码部分,然后单击
{ }将其标记为代码。 -
你posted too much code;可能是因为您不知道导致问题的原因。你haven't provided debugging details。所有这些都表明你有 done no debugging Yu 需要在发布到 Stack Overflow 之前这样做。
-
如果您为读者分词,您可能会发现您的代码更具可读性。例如,将
dernierTirageACompter与derniertirageacompter进行比较。前者更具可读性 -
目前尚不清楚您发布的代码中哪些问题不起作用,更不用说确切的问题是什么,也不清楚您需要什么具体的帮助。请重写您的问题,使其包含正确的minimal reproducible example - 即一个代码示例,其中only 只是演示问题所需的代码,并解释确切什么错误消息(s) 你得到什么,具体你需要什么帮助。
-
在
int derniertirageacompter = tirages.Count;上面的行中,您有var tirages = new List<Tirage>();。当Console.WriteLine($"dernier tirage: {derniertirageacompter}");出现时,您还没有向列表中添加任何内容,那么为什么计数会> 0?另外,我注意到,在 Tirage 中,您将 B1、B2 等声明为int,然后使用Convert.ToInt16。int是 Int32。请参阅docs.microsoft.com/en-us/dotnet/csharp/language-reference/… 了解更多信息。