中易网

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

答案:2  悬赏:20  
解决时间 2021-01-14 23:20
  • 提问者网友:战魂
  • 2021-01-14 10:39
head first java里的一道题,java代码编译时显示:以外的类型,还有private访问错误,为什么???
最佳答案
  • 二级知识专家网友:底特律间谍
  • 2021-01-14 12:01
首先,集合类(或者叫容器)不能放基本数据类型,必须是类。所以应把所有的ArrayList改成ArrayList
其次,被private修饰的属性(变量)都不能被其它类访问,改成其它权限即可。
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{
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;
}
}
全部回答
  • 1楼网友:胯下狙击手
  • 2021-01-14 12:08
我暂时保留我的看法!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息