Java 塞特被告知没有';它不能被创造,但它已经被创造了

Java 塞特被告知没有';它不能被创造,但它已经被创造了,java,setter,Java,Setter,我已经做了一个类似这样的程序 玩家被添加到一个数组中 然后他们都被安排在一对一的位置上(就像一场足球比赛,每个队都在比赛) 然后将其随机分组 程序应显示最终结果(如果你赢了一场比赛,你将得到3分)等 我一直从Eclipse收到一条消息,说“.setScore尚未编码 TestGame.java public class TestGame { static Players ceri = new Players("Ceri", 0); static Players harry =

我已经做了一个类似这样的程序

  • 玩家被添加到一个数组中
  • 然后他们都被安排在一对一的位置上(就像一场足球比赛,每个队都在比赛)
  • 然后将其随机分组
  • 程序应显示最终结果(如果你赢了一场比赛,你将得到3分)等
  • 我一直从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;
        }
    }
    

    您的代码有几个问题,但它们都与未定义的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);
        }
    }
    

    所有玩家都是TestGame类的静态成员。他们现在只能访问玩家的静态方法。这就是为什么会出现错误。

    首先,存在编译错误。请参阅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语句使用此格式。

    您使用的是
    ==
    而不是
    .equals
    。请参见此处:@jhobbie我知道如何使用。equals工作等等,但是为什么setter不工作?另外,变量的类型是什么
    ceri、harry、mathew等?它们在哪里声明,如何声明?这可能会影响编译r的错误。@user3870022它不起作用,因为
    name==“ceri”
    不起任何作用。其他所有的名字也一样。不起任何作用,我的意思是它不起你想要的作用。保持静态关键字不变,试试
    TestGame.ceri.setScore(3)
    等。我把它们都从静态改为静态。现在我会出错,因为“联盟”“也是静态的,然后我去掉了静态,还有更多的错误。虽然你的答案是正确的,但这也表明使某个静态的东西对其他对象不可见,这是错误的。最后一个问题,我的程序在询问一个匹配项后突然终止。。hmm@user3870022是的。在游戏类的while循环中有一个返回null。@user3870022:调试器是您可以使用的最好的工具之一。学习如何使用它,至少在基本水平上。它可以让您逐行运行代码,在运行中查看和更改变量,以及其他一些事情。如果您使用了调试器,您会很快发现返回null
    if(name.equals("ceri")){
        TestGame.ceri.setScore(3);
    }