我正在尝试自己学习Java,而我正在解决问题。 我正在创建一个程序,该程序从用户获取输入以生成两个Team对象,然后计算获胜者。每个团队都有一个名称和位置,表示为String对象,以及表示为double值的攻防能力,使用luck()方法在0和1之间随机初始化。
import java.util.Scanner;
public class Team {
public String name;
public String location;
public double offense;
public double defense;
public String awayName;
public String awayLocation;
public double awayOffense;
public double awayDefense;
/**
* Create a team with specified name and location, and with offense and defense capabilities
* randomly initialized using the luck() method.
*/
public Team(String name, String location) {
this.name = name;
this.location = location;
}
/**
* 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) {
home = (this.offense + this.defense + 0.2) * this.luck();
away = (visitor.offense + visitor.defense) * visitor.luck();
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>
*/
public static void main(String[] args) {
System.out.println("Enter name and location for home team (on separate lines)");
Scanner tn = new Scanner(System.in);
Team team = new Team("Name","Location");
team.name = tn.nextLine();
Scanner tl = new Scanner(System.in);
team.location = tl.nextLine();
Team home = new Team(team.name, team.location);
System.out.println("Enter name and location for away team (on separate lines)");
Scanner atn = new Scanner(System.in);
team.awayName = atn.nextLine();
Scanner atl = new Scanner(System.in);
team.awayLocation = atl.nextLine();
Team away = new Team(team.awayName, team.awayLocation);
System.out.println("Home team is: " + team.name+ " from " + team.location + " rated <home_team_offense> (offense) + <home_team_defense> (defense)");
System.out.println("Away team is: " + team.awayName+ " from " + team.awayLocation + " rated <home_team_offense> (offense) + <home_team_defense> (defense)");
// Create a home and an away team object with the given data.
// Call the home team's play() method with the away team as visitor parameter.
// Print out the winner.
评论的部分告诉我我需要做什么,虽然我不知道从哪里开始。我被困住了,认为到目前为止我所做的事情可能都不正确,有人能给我一个正确的方向吗?请帮忙! 谢谢!
答案 0 :(得分:2)
你自己走好路。
一旦你输入了攻击和防御的团队统计数据(为什么不将它们添加到构造函数参数中?),你可以像这样相互对战:
Team winner = home.play(away);
你的游戏方法应该是这样的:
Team play(Team visitor) {
double home = (this.offense + this.defense + 0.2) * this.luck();
double away = (visitor.offense + visitor.defense) * visitor.luck();
Team winner = (home > away ? this : visitor);
return winner;
}
你问过如何随机设置评级 - 如果你想让它只发生一次,构造函数将是一个理想的地方:
public Team(String name, String location) {
this.name = name;
this.location = location;
awayOffense = luck();
awayDefense= luck();
}
答案 1 :(得分:0)
我可能错了,但你没有用一个值初始化进攻和防守。 这样可能会更好:
public Team(String name, String location, double offence, double defence) {
this.name = name;
this.location = location;
this.offence = offence;
this.defence = defence;
}