英文:
class is public,should be declare in a file named SimpleDotCom.java?
问题
我根据我的《Head First Java》一书编写了以下内容:
public class SimpleDotComTestDrive{
public static void main(String[] args){
SimpleDotCom dot = new SimpleDotCom();
int[] locations = {2, 3, 4};
dot.setLocationCells(locations);
String userGuess = "2";
String result = dot.checkYourself(userGuess);
}
}
public class SimpleDotCom{
int[] locationCells;
int numOfHits = 0;
public void setLocationCells(int[] locs){
locationCells = locs;
}
public String checkYourself(String stringGuess){
int guess = Integer.parseInt(stringGuess);
String result = "miss";
for(int cell : locationCells){
if(guess == cell){
result = "hit";
numOfHits++;
break;
}
}
if(numOfHits == locationCells.length){
result = "Kill";
}
System.out.println(result);
return result;
}
}
**这是错误提示**
类 SimpleDotCom 是 public 的,应声明在名为 SimpleDotCom.java 的文件中
如果我尝试将文件保存为 SimpleDotCom 类,我仍然会得到类似“缺少期望的括号”的错误。
![1]
英文:
I'm write everything according to my book Head First Java
public class SimpleDotComTestDrive{
public static void main(String[] rip ){
SimpleDotCom dot = new SimpleDotCom();
int[] locations ={2,3,4};
dot.setLocationCells(locations);
String userGuess ="2";
String result = dot.checkYourself(userGuess);
}
}
public class SimpleDotCom{
int[] locationCells;
int numOfHits=0;
public void setLocationCells(int[] locs){
locationCells=locs;
}
public String checkYourself(String stringGuess){
int guess= Integer.parseInt(stringGuess);
String result="miss";
for(int cell : locationCells){
if(guess== cell){
result = "hit";
numOfHits++;
break;
}
}
if(numOfHits == locationCells.length){result="Kill";}
System.out.println(result);
return result;
}
}
This is the Error
class SimpleDotCom is public, should be declared in a file named SimpleDotCom.java
AND if i tried to save my file on the SimpleDotCom class i still got error like ename expected(brackets).
答案1
得分: 0
只允许在文件中有一个公共类。.java 文件的名称应与公共类相同。在您的情况下,文件应命名为 "SimpleDotComTestDrive.java"。
您的代码中还有另一个问题 - 您有两个公共类。请从其中一个类中删除 "public" 修饰符。
或者
您可以从两个类中都删除 "public" 修饰符,并随意命名文件。
英文:
It is allowed to have only ONE public class in file. The name of .java file should be the same as public class. In your case file should be named as "SimpleDotComTestDrive.java"
There is another issue in your code - you have two public classes. Please remove "public" modifier from one of classes.
OR
You could remove "public" modifier from both classes and name file as you wish.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论