我有一个文本文件数据集,格式如下(分隔符是制表符)。
0 762354
1 645645
2 4356743
3 576899063
4 64378
.....
我读了它并将它保存在数组中:
for (int klk = 0; klk <= 92159; klk++)
{
lineuserori = fileuserori.ReadLine();
if (!string.IsNullOrEmpty(lineuserori))
{
string[] valuesiesi = lineitemori.Split('\t');
int useridori;
foreach (string value in valuesiesi)
{
useridori = Convert.ToInt32(valuesiesi[1]);
d[klk] = useridori;
}
}
}
现在,我想读取一个输入并在array d
中搜索它,如果数字中存在数字,我会进行计算,如果它不在数组show MessageBox.Show("Error");
中,问题是,它总是显示每个输入的输出(即使数组中不存在输入),也从不显示MessageBox.Show("Error");
{
int sc = Convert.ToInt32(txtbx_id.Text);
int n = Convert.ToInt32(txtbx_noofrecomm.Text);
for (int yu = 0; yu <= 92161; yu++)
{
int wer = d[yu];
if (wer == sc)
{
userseq = yu;
break;
}
}
if (userseq >= 0 && userseq <= 92161)
{
var results = new List<float>(1143600);
for (int z = 0; z < 1143600; z++)
{
results.Add(dotproduct(userseq, z));
}
var sb1 = new StringBuilder();
foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n))
{
sb1.AppendFormat(CultureInfo.InvariantCulture, "{0}: {1}", c[resultwithindex.Index], resultwithindex.result);
sb1.AppendLine();
}
MessageBox.Show(sb1.ToString());
}
if (userseq < 0 || userseq > 92161)
{
MessageBox.Show("Error");
}
}
任何想法
由于
答案 0 :(得分:0)
在创建数组的代码中,lineitemori
是否正确?
我想,它应该是lineuserori
。
替换它:
string[] valuesiesi = lineitemori.Split('\t');
使用:
string[] valuesiesi = lineuserori.Split('\t');
我在下面写了代码。 (某些行使用未知变量注释掉)
输入762354
到txtbx_id
并单击按钮1然后出现空消息框
输入76235
到txtbx_id
并单击button1,然后MessageBox说'错误'。
public partial class Form1 : Form {
// form have three controls txtbx_id, txtbx_noofrecomm and button1.
int[] d = new int[92162];
string data =
"0\t762354\n"
+"1\t645645\n"
+"2\t4356743\n"
+"3\t576899063\n"
+"4\t64378\n";
public Form1() {
InitializeComponent();
using (var fileuserori = new StringReader(data)) { // use StringReader instead of StreamReader
string lineuserori = "";
for (int klk = 0; klk <= 92159; klk++) {
lineuserori = fileuserori.ReadLine();
if (!string.IsNullOrEmpty(lineuserori)) {
// string[] valuesiesi = lineitemori.Split('\t');
string[] valuesiesi = lineuserori.Split('\t');
int useridori;
foreach (string value in valuesiesi) {
useridori = Convert.ToInt32(valuesiesi[1]);
d[klk] = useridori;
}
}
}
}
}
private void button1_Click(object sender, EventArgs e) {
var userseq = -1;
int sc = Convert.ToInt32(txtbx_id.Text);
int n = Convert.ToInt32(txtbx_noofrecomm.Text);
for (int yu = 0; yu <= 92161; yu++) {
int wer = d[yu];
if (wer == sc) {
userseq = yu;
break;
}
}
if (userseq >= 0 && userseq <= 92161) {
var results = new List<float>(1143600);
for (int z = 0; z < 1143600; z++) {
// results.Add(dotproduct(userseq, z));
}
var sb1 = new StringBuilder();
foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n)) {
// sb1.AppendFormat(CultureInfo.InvariantCulture, "{0}: {1}", c[resultwithindex.Index], resultwithindex.result);
// sb1.AppendLine();
}
MessageBox.Show(sb1.ToString());
}
if (userseq < 0 || userseq > 92161) {
MessageBox.Show("Error");
}
}
}