无法解析为java程序中的变量
当我编译这段代码时,我得到的唯一错误是“winner不能解析为变量”或“令牌上的语法错误”winner,删除这个令牌“我理解这个错误与我的if-else语句有关,但我不知道我做错了什么,或者我只是用了错误的方式。有没有更好的方法在随机统计的两队之间取得胜利无法解析为java程序中的变量,java,Java,当我编译这段代码时,我得到的唯一错误是“winner不能解析为变量”或“令牌上的语法错误”winner,删除这个令牌“我理解这个错误与我的if-else语句有关,但我不知道我做错了什么,或者我只是用了错误的方式。有没有更好的方法在随机统计的两队之间取得胜利 public class Team { /** * The name of the team. */ public String name; /** * The location of
public class Team {
/**
* The name of the team.
*/
public String name;
/**
* The location of the team.
*/
public String location;
/**
* The offensive strength of the team.
*/
public double offense;
/**
* The defensive strength of the team.
*/
public double defense;
/**
* Create a team with specified name and location, and with offense and defense capabilities
* randomly initialized using the luck() method.
*
* @param name
* @param location
*/
public Team(String name, String location, double offense, double defense) {
this.name = name;
this.location = location;
this.offense = luck();
this.defense = luck();
}
/**
* The luck() method returns a random value between 0 and 1, using Math.random().
*
* @returns a real value in range [0,1]
*/
public double luck() {
return Math.random();
}
/**
* Run a competition against the specified visiting team
*
* @param other the team to play against
* @returns the winner team
*/
Team play(Team visitor) {
double home = (this.offense + this.defense + 0.2) * this.luck();
double away = (visitor.offense + visitor.defense) * visitor.luck();
if (home > away) {
}
else {
away = winner;
}
return winner;
}
/**
* Run a competition between two teams specified on standard input.
* Print statistics of the winner.
* <p>
* Each team is read in the following format :
* <pre>
* <name>
* <location>
* </pre>
* (Note that name and location should be separate lines for each team)
*
* @param args can be ignored.
*/
public static void main(String[] args) {
// Be sure to follow the same printing command to ask user
// for name and location of two teams (e.g, home and away)
System.out.println("Enter name and location for home team (on separate lines)");
// Get input from user for home team.
// Create a home team object with the given data.
Scanner in = new Scanner(System.in);
team = in.nextLine();
location = in.nextLine();
Team home = new Team(name, location, offense, defense);
System.out.println("Enter name and location for away team (on separate lines)");
// Get input from user for away team.
// Create an away team object with the given data.
team = in.nextLine();
location = in.nextLine();
Team away = new Team(name, location, offense, defense);
// Print out home team information.
System.out.println("Home team is: " + home.name + " from " + home.location + " rated <home_team_offense> (offense) + <home_team_defense> (defense)");
// Print out away team information.
System.out.println("Away team is: " + away.name + " from " + away.location + " rated <away_team_offense> (offense) + <away_team_defense> (defense)");
// Call the home team's play() method with the away team as visitor parameter.
Team winner = home.play(away);
// Print out the winner.
System.out.println("Winner is:" winner.name + "from " + winner.location + " rated <winner_team_offense> (offense) + <winner_team_defense> (defense)");
// (be sure to adhere to the format described below)
}
}
公共类团队{
/**
*团队的名称。
*/
公共字符串名称;
/**
*团队的位置。
*/
公共字符串位置;
/**
*球队的进攻力量。
*/
公开双重犯罪;
/**
*球队的防守力量。
*/
公共双重防卫;
/**
*创建一个具有指定名称和位置以及进攻和防守能力的团队
*使用luck()方法随机初始化。
*
*@param name
*@param位置
*/
公共团队(字符串名称、字符串位置、双重进攻、双重防御){
this.name=名称;
这个位置=位置;
这句话的意思是:冒犯=运气;
这个。防御=运气();
}
/**
*luck()方法使用Math.random()返回一个介于0和1之间的随机值。
*
*@返回[0,1]范围内的实际值
*/
公共双运{
返回Math.random();
}
/**
*与指定的客队进行比赛
*
*@param其他团队将与之比赛
*@返回冠军队伍
*/
团队游戏(团队访客){
双本垒打=(this.进攻+this.防守+0.2)*this.luck();
加倍进攻=(访问者.进攻+访问者.防守)*访问者.运气();
如果(主页>远离){
}
否则{
客场=赢家;
}
返回赢家;
}
/**
*在标准输入指定的两个团队之间进行比赛。
*打印获胜者的统计数据。
*
*每个团队的阅读格式如下:
*
*名字
*位置
*
*(请注意,每个团队的名称和位置应为单独的行)
*
*@param args可以忽略。
*/
公共静态void main(字符串[]args){
//确保按照相同的打印命令询问用户
//两支球队的名称和位置(如主客场)
System.out.println(“输入主队的名称和位置(在单独的行中)”;
//从用户处获取主团队的输入。
//使用给定数据创建主团队对象。
扫描仪输入=新扫描仪(系统输入);
team=in.nextLine();
location=in.nextLine();
主场队=新球队(名称、位置、进攻、防守);
System.out.println(“输入客场球队的名称和位置(在单独的行中)”;
//从用户处获取客场团队的输入。
//使用给定数据创建客场团队对象。
team=in.nextLine();
location=in.nextLine();
客场团队=新团队(名称、位置、进攻、防守);
//打印出主队信息。
System.out.println(“主队是:“+Home.name+”来自“+Home.location+”评级(进攻)+(防守)”);
//打印团队信息。
System.out.println(“客场球队是:“+Away.name+”来自“+Away.location+”评级(进攻)+(防守)”);
//调用主队的play()方法,将客队作为访问者参数。
团队赢家=主场。比赛(客场);
//打印出获胜者。
System.out.println(“获胜者是:“+Winner.location+”中的“Winner.name+”,“评级(进攻)+(防守)”);
//(请务必遵守以下所述格式)
}
}
您没有宣布获胜者。像这样更改play()
方法
/**
* Run a competition against the specified visiting team
*
* @param other the team to play against
* @returns the winner team
*/
Team play(Team visitor) {
double home = (this.offense + this.defense + 0.2) * this.luck();
double away = (visitor.offense + visitor.defense) * visitor.luck();
// depending on the score, return winning team.
return (home > away)? this : visitor;
}
或者,如果您只想有一个返回值
(这是个好主意)-
<>最后,你可能想考虑如果分数相同,即“代码> HOL==WOR/CODE>>会发生什么。如果得分相等,当前代码将使
访问者
获胜 正如@Sotirios Delimanolis所指出的,问题在于winner
是静态方法main
中的局部变量,而不是类成员变量(也称为字段),因此它在其外部不可见。winner在main之前不会被初始化。我认为你的方法play()是错误的。应写为:
或者类似的东西。
还请注意,您最初将(双倍)设置为团队赢家(在主方法之外不存在的团队)考虑
main()
方法中Winner
变量的范围。@SotiriosDelimanolis将您的评论作为answer@LexLythius你们知道了。哇,谢谢你们这么快的回答,伙计们!这解决了我第一个错误的问题,但我仍然得到“令牌上的语法错误”winner,“删除此令牌”错误,一个选项显然是删除它,但我需要它来打印谁是赢家。如何解决这个问题?“Onereturn
only”是一个分裂规则。有些人认为这很重要,而另一些人则完全拒绝。在这种情况下,这没有多大区别,但在其他情况下,这是相当深刻的。
/**
* Run a competition against the specified visiting team
*
* @param other the team to play against
* @returns the winner team
*/
Team play(Team visitor) {
double home = (this.offense + this.defense + 0.2) * this.luck();
double away = (visitor.offense + visitor.defense) * visitor.luck();
// depending on the score, return winning team.
return (home > away)? this : visitor;
}
Team play(Team visitor) {
Team winner;
double home = (this.offense + this.defense + 0.2) * this.luck();
double away = (visitor.offense + visitor.defense) * visitor.luck();
if (home > away) {
winner = this;
}
else {
winner = visitor
}
return winner;
}