JAVA猜數字遊戲 程式碼解釋
import java.io.*;
{
private static short cpu[] = new short[4];
private static short player[] = new short[4];
private static boolean num[] = new boolean[10];/
private static short i,j,k,temp,round,a,b,str_length;
private static final short MAX_ROUND = 10;//回合數
private static boolean ok = true;
public static short rand(short n)
{
short temp;
do
{
temp = (short)(Math.random() * 10);
}
while(temp > n);
return temp;
}
public static void show(short a[])
{
for(int i = 0; i < a.length; i ++)
System.out.print(a[i]);
}
public static void main(String args[])throws IOException
{
BufferedReader buf;
String str = "0000";
buf = new BufferedReader(new InputStreamReader(System.in));/
for(int d = 0; d < 10; d ++)
{
num[d] = false;
}
i = 0;
do
{
do
{
j = rand((short)10);
}
while(num[j] == true);
num[j] = true;
cpu[i] = j;
i++;
}
while(i < 4);
System.out.println("最多猜" + MAX_ROUND + "次\n");
round = 1;
do
{
try
{
System.out.print("請輸入4位數->");
str = buf.readLine();
str_length = (short)str.length();
if(str_length != 4)
{
System.out.println("\n數值超過(不足),請重新輸入!\n");
continue;
}
temp = Short.parseShort(str);
for(k = 3; k >= 0; k--)
{
player[k] = (short)(temp % 10);
temp /= 10;
}
}
catch(NumberFormatException e)
{
System.out.println("數值格式不對,請重新輸入!\n");
continue;
}
finally
{
temp = 0;
}
for(int d = 0; d < 4; d++)
{
for(int e = d + 1; e < 4; e++)
{
if(player[d] == player[e])
ok = false;
for(int i = 0 ;i < 4; i++)
player[i] = 0;
continue;
}
else if(ok == true)
{
System.out.print("\n" + round + " round...");
round ++;
show(player);
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
System.out.println(" " + a + "A" + b + "B\n");
if(a == 4)
{
System.out.println("你猜中了!!");
break;
}
a = 0;
b = 0;
}
while(round <= MAX_ROUND);
if(round > MAX_ROUND)System.out.print("超過" + MAX_ROUND + "回合!!");
System.out.print("答案是");
show(cpu);
System.out.println();
}
可以幫我解釋一下每一行是在說甚麼嘛?
用雙斜線在後面做註釋就好
例如
private static final short MAX_ROUND = 10;//回合數
像這樣大概講一下就可以了!!!!!
拜託了各位
6 個解答
- 吉他Lv 51 0 年前最佳解答
這是我5年前寫的程式,為什麼到你手上注解全不見了?給你原版程式吧,還有copy別人程式請跟原作者說一下,謝謝。
我手邊還保有原始檔,最後修改日期是2004-11-07....
我把原程式有加注解的貼給你,這裡貼不上,我放在日誌…
猜數字
圖片參考:http://0.blog.xuite.net/0/3/6/8/15355732/blog_4425...
參考資料: 你發的程式原作者是我... - 匿名使用者6 年前
明明他是在求註解,如果是從原處copy來的他也不需要發問吧~!! 一定是另有他人
- 您覺得這個回答如何?您可以登入為回答投票。
- 匿名使用者1 0 年前
哈哈哈...到他手上註解全不見。
這厲害了!!