Re: [問題] 請問一個關於while的用法
※ 引述《liunate (測試)》之銘言:
: ※ 引述《blestadsl (松鼠)》之銘言:
: : Assign(X,9) 會回傳 0、1 或 True、False
: : 當 while(1)&while(True)就會繼續做 Assign(X,9)
: : 反之則離開while不做 Assign(X,9)
: : 應該是這樣吧!?^o^
: 我覺得比較奇怪的地方是
: While使用()作為限定範圍用,因為在C, C#, Java中使用{},而在VB.NET中使用
: End While。
我再寫的問題是一個3*3的魔術方陣
排列1~9 使不管是橫的.直的或斜的總和均相同
ex: 4 3 8
9 5 1
2 7 6
下面是原本完整的程式碼 還有一些我其他不太清楚的問題
static void Main(string[] args)
{
int[,] X=new int [3,3];
X[1,1]=5;
while(!Assign(X,9));
}
static bool Assign(int[,] X,int count)
{
int i,j;
if(count==5)
{
if(Check(X))
{
Show(X);
Console.WriteLine();
return true;
}
else
return false;
}
當count = 5之後 下面的程式還會執行嗎
還是說 return 完之後就跳出這個method嗎
如果執行下面的程式了 又是如何終止整個程式
Random r=new Random();
while(true)
{
i=r.Next(0,3);
j=r.Next(0,3);
if(X[i,j]==0) break;
}
X[i,j]=count;
X[2-i,2-j]=10-count;
bool ra=Assign(X,count-1);
X[i,j]=0;
X[2-i,2-j]=0;
這邊 什麼時候會執行阿
return ra;
}
static bool Check(int[,] X)
{
//X=new int[,] {{8,1,6},{3,5,7},{4,9,2}};
bool ok=true;
for(int i=0;i<3;i++)
{
int sum=0;
for(int j=0;j<3;j++)
sum+=X[i,j];
if(sum!=15) ok=false;
sum=0;
for(int j=0;j<3;j++)
sum+=X[j,i];
if(sum!=15) ok=false;
}
if(X[0,0]+X[1,1]+X[2,2]!=15) ok=false;
if(X[2,0]+X[1,1]+X[0,2]!=15) ok=false;
return ok;
}
static void Show(int[,] X)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
Console.Write(X[i,j]);
Console.WriteLine();
}
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.216.248.18
討論串 (同標題文章)
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章