类是公共的,应该在一个名为SimpleDotCom.java的文件中声明。

huangapple go评论69阅读模式
英文:

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).

类是公共的,应该在一个名为SimpleDotCom.java的文件中声明。

答案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.

huangapple
  • 本文由 发表于 2020年9月9日 03:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63800282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定