0

## C# 兩個Array每個位置比較

ex 5278 myArray[0]=5 ,myArray[1]=2 ....

``````A = 0;
B = 0;

for (int i = 0; i < 4; i++)
{
if (myArray[i].ToString() == UserArray[i].ToString())
{
A += 1;
}

for (int j = 0; j < 4; j++)
{
if(i!=j)
{
if (myArray[i].ToString() == UserArray[j].ToString())
{
B += 1;
}
}
}
}
``````

### 2 個回答

2

iT邦大師 1 級 ‧ 2019-10-01 11:39:30

4個數字而已差異不大,

Zed_Yang iT邦新手 3 級 ‧ 2019-10-01 13:52:38 檢舉

Zed_Yang iT邦新手 3 級 ‧ 2019-10-01 15:22:46 檢舉

``````using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 猜數字小遊戲
{
public partial class Form1 : Form
{
bool IsWin = false;
int FailCount = 0;
int A = 0;
int B = 0;
//答案
Dictionary<int, int> myDictionary = new Dictionary<int, int>();
//使用者輸入的
ArrayList UserArray = new ArrayList();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
clearUI();
}

private void button1_Click(object sender, EventArgs e)
{
clearUI();
}

private void clearUI()
{
label1.Visible = false;
label1.Text = "答案是:";
IsWin = false;
FailCount = 0;
A = 0;
B = 0;
textBox1.Clear();
label3.Visible = false;
myDictionary.Clear();
UserArray.Clear();
listBox1.Items.Clear();
int number = 0;
System.Random num = new Random();

//初始化答案物件
myDictionary.Clear();
//前面是數字，後面是位置，預設值是0
for (int i = 0; i < 10; i++)

for (int i = 0; i < 4; i++)
{
do
{
number = num.Next(0, 10);
}
//初始化答案
while (myDictionary[number] != 0);
myDictionary[number] = i + 1;
label1.Text += number.ToString();
}

}

private void button2_Click(object sender, EventArgs e)
{
Close();
}

private void button3_Click(object sender, EventArgs e)
{
Ans();
}

private void Ans()
{
UserArray.Clear();

if (!IsWin)
{
#region 檢核
if (textBox1.Text.Count() != 4)
{
MessageBox.Show("只可以輸入四個數字,請重新輸入!!");
textBox1.Clear();
textBox1.Focus();
return;
}
try
{
var TES = Convert.ToInt32(textBox1.Text);
}
catch (Exception e)
{
MessageBox.Show("只可以輸入數字,請重新輸入!!");
textBox1.Clear();
textBox1.Focus();
return;
}
for (int i = 0; i < 4; i++)
{
}
if (UserArray[0] + "" == UserArray[1] + "" || UserArray[0] + "" == UserArray[2] + "" ||
UserArray[0] + "" == UserArray[3] + "" || UserArray[1] + "" == UserArray[2] + "" ||
UserArray[1] + "" == UserArray[3] + "" || UserArray[2] + "" == UserArray[3] + "")
{
MessageBox.Show("不可以輸入重複數字,請重新輸入!!");
textBox1.Clear();
textBox1.Focus();
return;
}
#endregion

FailCount += 1;

A = 0;
B = 0;

for (int i = 0; i < 4; i++)
{
//該數字的位置
int value = myDictionary[Convert.ToInt32(UserArray[i])] - 1;
if (value == i)
A += 1;
else if (value >= 0)
B += 1;
}
label3.Text = A.ToString() + " A " + B.ToString() + " B ";
label3.Visible = true;
listBox1.Items.Add(textBox1.Text + " " + label3.Text);
listBox1.Focus();
listBox1.TopIndex = FailCount - 1;
textBox1.Focus();
textBox1.Clear();
if (A == 4)
{
IsWin = true;
MessageBox.Show("恭喜你,嘗試了" + FailCount.ToString() + "次,終於成功了!!");
label1.Visible = true;
}
}
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button3.Focus();
button3_Click(sender, e);
}
}

}
}
``````

Zed_Yang iT邦新手 3 級 ‧ 2019-10-02 13:17:15 檢舉

``````myDictionary.Clear();
for (int i = 0; i < 10; i++)

for (int i = 0; i < 4; i++)
{
do
{
number = num.Next(0, 10);
}
while (myDictionary[i] != 0);
myDictionary[number] = i + 1;
label1.Text += number.ToString();
}
``````

thx

p.s. 我發現我沒有寫錯,

3

iT邦新手 4 級 ‧ 2019-10-01 12:40:11

``````for (int i = 0; i < 4; i++)
{
int index = Array.IndexOf(myArray, UserArray[i]);
if(index > -1)
{
if(index == i)
{
A += 1;
}
else
{
B += 1;
}
}
}
``````

YoChen iT邦研究生 5 級 ‧ 2019-10-01 13:16:19 檢舉

Zed_Yang iT邦新手 3 級 ‧ 2019-10-01 14:00:12 檢舉
``````for (int i = 0; i < 4; i++)
{
int index = myArray.IndexOf(UserArray[i]);
if(index > -1)
{
if(index == i)
{
A += 1;
}
else
{
B += 1;
}
}
}
``````