变量不在范围内(我认为),但不确定如何解决它

时间:2015-10-21 00:50:17

标签: java class variables types scope

我正在做的只是练习测试,我认为它做得很好,但我得到了,我认为是一个范围问题。它说,“团队无法解决变量类型”,我尝试了一些我认为会修复它的东西,但它们没有用。这是代码:

import java.util.Scanner; public class fundamentalsofgame {

public String hteam;
public String cteam;

public String teams(String hometeam, String compteam){
    String hteam = hometeam;
    String cteam = compteam;


    return "The teams are " + hteam + " vs " + cteam;

}

public static void main(String[] args){

    Scanner scanner = new Scanner(System.in);
    String hometeam;
    String awayteam = "New England Cheatriots";

    hometeam = scanner.next();

    teams team = new teams(hometeam, awayteam); //error

}

}

1 个答案:

答案 0 :(得分:0)

teams是一种方法,而不是您的类名,而是fundamentalsofgame。因此,您需要创建fundamentalsofgame的对象并在其上调用teams方法。改变这个:

teams team = new teams(hometeam, awayteam); //error

fundamentalsofgame obj = new fundamentalsofgame();
fundamentalsofgame.teams(hometeam, awayteam);