这是一个极其简单的游戏:我们输入数字,游戏会检测我们是否击中一个网站,这个网站被击中3次会被“杀死”,这个网站连接3个变量,比如这个网站有1、2、3这3个格子,或者3、4、5这3个格子,又或者1、1、1这三个一样的格子(这是个bug,先不管),最后游戏会提示我们猜了多少次。
前面我在写SimpleDotcom的时候出现了一个问题,但是不知道是为什么,我在网上查了查但是查不到,没有解决方案,我先把原先的代码放过来吧:
public class SimpleMain{
public static void main(String[] args){
int numOfGuess = 0;//记录玩家猜测次数的变量
GameHelper helper = new GameHelper();//我们会先写出这个类来取得玩家的输入,现在先假装这是Java提供的
SimpleDotCom theDotCom = new SimpleDotCom();//创建Dot Com对象
int randomNum = (int)(Math.random() * 5);//用随机数产生第一格的位置,然后以此制作出数组
int[] locations = {randomNum, randomNum + 1, randomNum + 2};//用随机数产生第一格的位置,然后以此制作出数组
theDotCom.setLocationCells(locations);//向类中赋值数组
boolean isAlive = true;//达康是否存活
while(isAlive){
String guess = helper.getUserInput("Enter a number:");//取得玩家输入的字符串
String result = theDotCom.checkYourself(guess);//检测玩家猜测并将结果放在String中
numOfGuess++;
if(result.equals("kill")){
isAlive = false;
System.out.println("你猜了"+numOfGuess+"次 ");
}
}
}
}
import java.io.*;
class GameHelper{
public String getUserInput(String prompt){
String inputLine = null;
System.out.print(prompt + " ");
try{
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
if(inputLine.length() == 0)
return null;
}
catch(IOException e){
System.out.println("IOException: " + e);
}
return inputLine;
}
}
class SimpleDotCom{
int[] locationCells;
int numOfHits = 0;
String checkYourself(String stringGuess){
int guess = Integer.parseInt(stringGuess);//把stringGuess(String)转换成int格式
String result = "miss";//设定结果是miss(没打中)
for(int cell : locationCells){//以循环对每个格子重复执行
if(guess == cell){//如果猜中
result = "hit";//结果是hit
numOfHits++;//打中次数加一
break;//此时直接跳出循环
}
}
if(numOfHits == locationCells.length){//如果打中次数与规定的次数相等
result = "kill";//这个达康被消灭
}
System.out.println(result);//输出结果
return result;//返回结果
}
void setLocationCells(int[] locs){
locationCells = locs;//把形参传入locationCells中
}
}
/*问题:
SimpleMain.java:25: 错误: 需要class, interface或enum
import java.io.*;
*/
这是第25行,少了class、interface或者enum。但我也不知道为什么,网上说是跟ANSI或者UTF-8有关,也有其他的,但到现在我也不明白怎么回事。但是!!!我第25行移到第一行就好了,顺利运行。
import java.io.*;
public class SimpleMain{
public static void main(String[] args){
int numOfGuess = 0;//记录玩家猜测次数的变量
GameHelper helper = new GameHelper();//我们会先写出这个类来取得玩家的输入,现在先假装这是Java提供的
SimpleDotCom theDotCom = new SimpleDotCom();//创建Dot Com对象
int randomNum = (int)(Math.random() * 5);//用随机数产生第一格的位置,然后以此制作出数组
int[] locations = {randomNum, randomNum + 1, randomNum + 2};//用随机数产生第一格的位置,然后以此制作出数组
theDotCom.setLocationCells(locations);//向类中赋值数组
boolean isAlive = true;//达康是否存活
while(isAlive){
String guess = helper.getUserInput("Enter a number:");//取得玩家输入的字符串
String result = theDotCom.checkYourself(guess);//检测玩家猜测并将结果放在String中
numOfGuess++;
if(result.equals("kill")){
isAlive = false;
System.out.println("你猜了"+numOfGuess+"次 ");
}
}
}
}
class GameHelper{
public String getUserInput(String prompt){
String inputLine = null;
System.out.print(prompt + " ");
try{
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
if(inputLine.length() == 0)
return null;
}
catch(IOException e){
System.out.println("IOException: " + e);
}
return inputLine;
}
}
class SimpleDotCom{
int[] locationCells;
int numOfHits = 0;
String checkYourself(String stringGuess){
int guess = Integer.parseInt(stringGuess);//把stringGuess(String)转换成int格式
String result = "miss";//设定结果是miss(没打中)
for(int cell : locationCells){//以循环对每个格子重复执行
if(guess == cell){//如果猜中
result = "hit";//结果是hit
numOfHits++;//打中次数加一
break;//此时直接跳出循环
}
}
if(numOfHits == locationCells.length){//如果打中次数与规定的次数相等
result = "kill";//这个达康被消灭
}
System.out.println(result);//输出结果
return result;//返回结果
}
void setLocationCells(int[] locs){
locationCells = locs;//把形参传入locationCells中
}
}
/*结果:
Enter a number: 1
miss
Enter a number: 2
miss
Enter a number: 3
miss
Enter a number: 4
hit
Enter a number: 5
hit
Enter a number: 6
kill
你猜了6次
*/
上面结果中数字是我瞎猜的,猜了六次才猜对,大家猜可能是其他结果,这个是简单的程序,bug很多,都是细节。希望大家知道是怎么回事的帮我看一下,谢谢你们了!
现在,我看的似乎有点明白了,在第6章155页,有这样一段话:
这是A的结果,必须放在最前面才行。。。我估计有可能是这个问题了。