老师在上课有讲解了以下的部分
但说得很快 XDD
要我们说明程式码及修改以下程式码Number Guess Game.java 部分的黑色粗框 for
来完成完整版的4位数猜数字游戏
1. 可由使用者选择所欲猜测的数字有几位
2. 使用者可查询过去所输入的猜测
3. 电脑可提供提示 (例如 : 1A2B)
4. 游戏排行榜
而这个猜数字的游戏 包含了 Number Guess Game.java / Answer.java 及 Main.java三个部分
(主要是修改Number Guess Game.java 部分及在执行时能印出步骤说明 例如: 你猜错了 继续加油 )
Number Guess Game.java 部分
package numberguessgame;
import java.util.Scanner;
import java.io.*;
import java.util.Vector;
import java.util.Random;
public class NumberGuessGame {
private int numberOfDigits;
private boolean isDupDigits=true;
int count=1;
Answer lastAnswer=null;
Vector possibleAnswerSet;
NumberGuessGame()
{
this(4);
}
NumberGuessGame(int d)
{
setNumberOfDigits(d);
possibleAnswerSet = new Vector();
}
public Answer generateAnswer()
{
Answer temp = new Answer(numberOfDigits);
temp.generate();
// System.out.println(temp.answer);
return temp;
}
public void start(Answer answer)
{
boolean exit=false;
Answer userGuess;
String outStr=null;
while(!exit && (count<100))
{
System.out.println("------------------------");
System.out.println("(" + count + ")");
// userGuess = getUserGuess();
userGuess = generateGuess(answer, outStr);
outStr = compare(userGuess,answer);
System.out.println(outStr);
if(outStr.equals("" + this.numberOfDigits + "A0B"))
exit=true;
count++;
}
System.out.println("Game Over");
}
public Answer generateGuess(Answer answer, String outStr)
{
Answer temp = new Answer(this.numberOfDigits);
System.out.print("Please Input Your Guess:");
if(count==1)
{
temp = generateAnswer();
lastAnswer = temp;
return temp;
}
else if(count==2)
{
Answer a = generateAnAnswer(answer, outStr);
lastAnswer =a;
System.out.println(a.answer);
return a;
}
..
访客只能看到部份内容,免费 加入会员 或由脸书 Google 可以看到全部内容