head first java 错误_head first java里的一道题,java代码编译时显示:以外的类型,还有private访问错误,为什么???...

这是一个简单的Java程序,实现了一个名为SimpleDotCom的游戏。玩家通过输入数字进行猜测,游戏会根据猜测结果返回'命中'或'错过'。游戏的目标是通过最少的猜测次数找出由三个随机数字组成的点阵。程序包括GameHelper类用于获取用户输入,以及SimpleDotCom类用于游戏逻辑和位置检查。
摘要由CSDN通过智能技术生成

importjava.io.*;importjava.util.ArrayList;publicclassSimpleDotComTestDrive{publicstaticvoidmain(String[]args){intnumofguess=0;GameHelperhelper=newGameHelper();SimpleDotCo...

import java.io.*;

import java.util.ArrayList;

public class SimpleDotComTestDrive{

public static void main(String[] args){

int numofguess=0;

GameHelper helper=new GameHelper();

SimpleDotCom theDotCom=new SimpleDotCom();

theDotCom.setLocationCells(theDotCom.locations);

boolean isAlive=true;

while(isAlive==true){

String guess=helper.getUserInput("enter a number");

String result=theDotCom.checkYourself(guess);

numofguess++;

if(result.equals("Kill")){

isAlive=false;

System.out.println("You took "+numofguess+" guesses");

}

}

}

}

class SimpleDotCom{

private ArrayList locations=new ArrayList();

public String checkYourself(String userguess){

String result="miss";

int guess=Integer.parseInt(userguess);

int index=locations.indexOf(guess);

if(index>0){

locations.remove(index);

result="Hit";

}

if(locations.isEmpty()){

result="Kill";

}

System.out.println(result);

return result;

}

public void setLocationCells(ArrayList a){

int randomnum1=(int)(Math.random()*5);

int randomnum2=randomnum1+1;

int randomnum3=randomnum2+1;

a.add(randomnum1);

a.add(randomnum2);

a.add(randomnum3);

}

}

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;

}

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值