我已经制作了一个类似于此的程序
我不断收到来自Eclipse的消息说" .setScore尚未编码..
TestGame.java
public class TestGame {
static Players ceri = new Players("Ceri", 0);
static Players harry = new Players("Harry", 0);
static Players matthew = new Players("Matthew", 0);
static Players james = new Players("James",0);
static Players kwok = new Players("Kwok",0);
static Players lewis = new Players("Lewis",0 );
static Game League = new Game();
int Ceri = 0;
public static void main(String[] args) {
League.addPlayers(ceri);
League.addPlayers(matthew);
League.addPlayers(james);
League.addPlayers(kwok);
League.addPlayers(lewis);
League.addPlayers(harry);
League.randomize();
League.Results();
}
}
Game.java
public class Game extends TestGame {
Scanner s = new Scanner(System.in);
private Players[] people = new Players[6];
private int counter = 0;
List<String> Matches = new ArrayList<String>();
public void addPlayers(Players obj){
people[counter] = obj;
counter++;
System.out.println(obj.getName());
}
public String randomize(){
for(int i = 0; i < people.length; i++){
for(int j = i + 1; j < people.length; j++){
if(people[i].equals(people[j].getName())){
continue;
} else {
Matches.add(people[i].getName() + " V " + people[j].getName());
}
}
}
return null;
}
public String Results(){
while(!Matches.isEmpty()){
int Game = (int)(Math.random() * Matches.size());
String Verses = (String)Matches.get(Game);
System.out.println(Verses);
System.out.println("Who has won?");
String name = s.nextLine();
//**ISSUE LIES HERE**
if(Verses.contains(name)){
if(name == "ceri"){
ceri.setScore(3);
} else if(name == "harry"){
harry.setScore(3);
}else if (name == "matthew"){
matthew.setScore(3)
} else if(name == "lewis"){
lewis.setScore(3)
}else if ( name == "james"){
james.setScore(3)
}else if(name == "kwok"){
kwok.setScore(3);
}
}
Matches.remove(Game);
System.out.println(one);
return null;
}
return null;
}
}
ECLIPSE SAYS .setScore ISNT。
Players.java
public class Players {
private String name;
private int score;
public Players(String name, int score){
this.name = name;
this.score = score;
}
public String getName(){
return this.name;
}
public int getScore() {
return this.score;
}
public void setScore(int score) {
this.score =+ score;
}
}
答案 0 :(得分:1)
所有玩家都是TestGame类的静态成员。他们现在只能访问玩家的静态方法。这就是你收到错误的原因。
答案 1 :(得分:0)
您的代码有几个问题,但它们都与setScore没有定义有关。
您在以下块中缺少分号。
if(Verses.contains(name)){
if(name == "ceri"){
ceri.setScore(3);
} else if(name == "harry"){
harry.setScore(3);
}else if (name == "matthew"){
matthew.setScore(3)
} else if(name == "lewis"){
lewis.setScore(3)
}else if ( name == "james"){
james.setScore(3)
}else if(name == "kwok"){
kwok.setScore(3);
}
}
您没有在上面的块中使用equals
进行字符串比较。
以下是更正后的版本:
if(Verses.contains(name)){
if(name.equals("ceri")){
ceri.setScore(3);
} else if(name.equals("harry")){
harry.setScore(3);
} else if (name.equals("matthew")){
matthew.setScore(3);
} else if(name.equals("lewis")){
lewis.setScore(3);
} else if ( name.equals("james")){
james.setScore(3);
} else if(name.equals("kwok")){
kwok.setScore(3);
}
}
答案 2 :(得分:0)
首先,有编译错误。请参阅merlin2011的答案以供参考。
关于你的实际问题,你的玩家(ceri,harry,matthew等)是TestGame的静态成员......但是没有公开曝光。这意味着其他对象无法看到它们,因此无法访问它们。将public
关键字添加到其中:
public class TestGame {
public static Players ceri = new Players("Ceri", 0);
public static Players harry = new Players("Harry", 0);
public static Players matthew = new Players("Matthew", 0);
public static Players james = new Players("James",0);
public static Players kwok = new Players("Kwok",0);
public static Players lewis = new Players("Lewis",0 );
...
}
现在它们已公开曝光,您需要正确引用它们。当你拨打setScore(3)
时,你的游戏类不知道什么是ceri,因为它不是那个类的成员。相反,你需要告诉它ceri所在的位置;在你的TestGame类中。作为参考,我还使用了传统的字符串相等比较函数。
if(name.equals("ceri")){
TestGame.ceri.setScore(3);
}
将此格式用于您拥有的其他IF语句。