【问题标题】:Variable not in scope (I think), but not sure how to fix it变量不在范围内(我认为),但不知道如何修复它
【发布时间】:2016-01-19 20:26:20
【问题描述】:

我正在做的只是为了测试而练习,我认为它做得很好,但我得到了,我认为这是一个范围问题。它说,“无法将团队解析为变量类型”,我尝试了一些我认为可以解决它的方法,但它们没有奏效。代码如下:

导入 java.util.Scanner; 公共类 basicsofgame {

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

}

}

【问题讨论】:

  • teams 是一种方法而不是构造函数。阅读文档教程
  • 另外,类名要大写。

标签: java class variables types scope


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢。我知道了。这是漫长的一天。
猜你喜欢
  • 2017-08-05
  • 2021-12-08
  • 2010-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多