我答有 XD
問佢係邊返工
之後佢叫我返到公司先 再坐公司車去遵理啲補習社幫手 ...
神乂怒火 發表於 2-12-2010 22:29 
good=]
做埋個program先再上一上黎
搵人幫我test下個program run唔run到= .=
依部機無java
import java.util.*;
public class xxx {
public static void main(String args[])
{
// int 所有要用既野
int dn;//dn=difficulty_num
int ds;//ds=difficulty_step
int in;//in=inputnum
int ye;//ye=yesno
int out;//out=outputnum
int ch;//ch=checkerro
int guess[]={0,0,0,0};//quwss
int answer[]={0,0,0,0};//answer
int bl;//b=black
int wh;//w=white
Scanner sc = new Scanner(System.in);
System.out.println("Welcome Mastermind ");
do{
String outprint = "";
do{
System.out.print("Choose the maximum number of steps : ");
ds = sc.nextInt();
System.out.print( ds < 1 ? "Your input is wrong!\n ":"");
}while (ds < 1);
do{
System.out.print("Choose the number of digits to guess :");
dn = sc.nextInt();
System.out.print( dn < 1 || dn > 9 ? "Your input is wrong!\n":"");
}
while (dn < 1 || dn > 9);
do{
System.out.print("Player VS Computer (0-Yes / 1-No)? ");
ye = sc.nextInt();
if (ye == 0){
System.out.println("Player2 Starts to guess ---\nType '0' to leave the game.");
for (int a=0;a<=3;a++){
answer[a] = (int)(Math.random()*dn + 1);}}
else if (ye == 1){
do{
System.out.print("Player1 input the answer: ");
in = ch = sc.nextInt();
for (int b=1000,a=0;a<=3 && b>=1;a++,b/=10){
answer[a] = in/b;
in %= b;
if (ch < 1111 || ch > 9999 || answer[a] < 0 || answer[a] > dn){
System.out.println("Your input is wrong!");
ch *= 10000;
break;}}
}while (ch < 1111 || ch > 9999);
for (int line=1;line<=60;line++){
System.out.println();}}
else
System.out.println("Please input 0 or 1 (0-yes / 1-No)!");
}while (ye != 1 && ye != 0);
for (int step=1;step <= ds;step++){
bl=0;
wh=0;
do{
System.out.print("Step " + step + ": Please input your guess: ");
if (in == 0)
break;
for (int a=0,b=1000;a<=3&&b>=1;a++,b/=10){
guess[a] = in/b;
in %= b;
if (out < 1111 || out > 9999 || guess[a] < 0 || guess[a] > dn){
System.out.println("Your input is wrong!");
out *= 10000;
break;}}
}while (out < 1111 || out > 9999);// outputnum <1111 or outputnum >9999
for (int a=0;a<=3;a++)
{
if (guess[a] == answer[a])
{
bl++;
answer[a] -= 100;
guess[a] -= 100;
}
}
for (int a=0;a<=3;a++)
{
if (guess[a] < 0)
continue;
for (int b=0;b<=3;b++)
{
if (a != b && guess[a] == answer)
{
wh++;//白
answer -= 100;
break;}
}
}
for (int c=0;c<=3;c++){
answer[c] = (answer[c]<0) ? answer[c]+100:answer[c];
}
outprint += "Guess " + step + ": " + out + " Black: " + bl + " White: " + wh + "\n";
System.out.print(out);
if (bl == 4){
System.out.print("You Win!");
break;
}
System.out.println("=================================\n");
if (step == ds && bl != 4 || out == 0)//step=difficulty_step black=4 or outputnum =0
{
System.out.print("You Lose! The answer is ");
for (int ans=0;ans<=3;ans++)
{
System.out.print(answer[ans]);
}
break;
}
}
do{
System.out.print("\nDo you want to continue (0-Yes / 1-No)? ");
ye = sc.nextInt();
System.out.print(ye != 1 && ye != 0 ? "Please input 0 or 1 (0-Yes / 1-No)!":"");
}
while (ye != 0 && ye != 1);
}
while (ye == 0);//yesno = 0
System.out.println("Bye Bye!");
}
} |