我在入门级Java课程中,我们已经分配了一个团队竞赛项目。我已经完成了大部分工作,但是当我编译它时,我得到一些错误说明:
3 errors found:
File: C:\Users\Brett\Documents\DrJava\Team.java [line: 70]
Error: The constructor Team(java.lang.String, java.lang.String, java.lang.String, java.lang.String) is undefined
File: C:\Users\Brett\Documents\DrJava\Team.java [line: 79]
Error: The constructor Team(java.lang.String, java.lang.String, java.lang.String, java.lang.String) is undefined
File: C:\Users\Brett\Documents\DrJava\Team.java [line: 80]
Error: The method competition(Team, Team) is undefined for the type Team
源代码
/**
* Project 1 -- Team Competition Simulator
*
* This program gets input from the user to make two Team objects and then
* calculates the winner.
*
* @Brett Donahue
* @lmf
* @16 September 2013
*/
import java.lang.Math.*;
import java.util.Scanner;
public class Team{
final int offense, defense;
final double luck;
final String name, location;
/**
* Class constructor
*
* @param n = name of the team to be created
* @param loc = location of the team to be created
* @param o = offensive rating of the team to be created
* range should be 0-100
* @param d = defensive rating of the team to be created
* range should be 0-100
* luck is to be randomly generated using java.lang.Math
* range: 0-1
*/
public Team(String n, String loc, int o, int d){
offense = o;
defense = d;
name = n;
location = loc;
luck = (double)(Math.random() * (1 - 0) + 0);
}
/**
* Perform the calculation explained on the project description
* Print out the ratings of each Team
* Print out the winning Team's location and name
* You do not have to worry about there being a draw
*/
public static int competion(Team home, Team away){
System.out.println(home.name + ": Offense " + home.offense + "Defense "+home.defense);
System.out.println(away.name + ": Offense " + away.offense + "Defense "+home.defense);
double ovr1 = (1/25.0)*(home.offense+home.defense+(0.2*(home.offense+home.defense))*home.luck) + 0.4;
double ovr2 = (1/25.0)*(away.offense+away.defense+(0.2*(away.offense+away.defense))*away.luck) + 0.4;
if(ovr1 > ovr2)
System.out.println("The "+home.name+" of "+home.location+" have won!");
else if(ovr1 < ovr2)
System.out.println("The "+away.name+" of "+away.location+" have won!");
return 0;
}
/**
* Prompt user for input
* Formatting = prompt "Name Location Offense Defense"
* Make two Team objects
* Compete
*/
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Team 1: Enter a Name.");
String n1 = scanner.nextLine();
System.out.println("Team 1: Enter a Location.");
String l1 = scanner.nextLine();
System.out.println("Team 1: Enter an Offensive Rating (0-100).");
String o1 = scanner.nextLine();
System.out.println("Team 1: Enter a Defensive Rating (0-100).");
String d1 = scanner.nextLine();
Team team1 = new Team(n1,l1,o1,d1);
System.out.println("Team 2: Enter a Name.");
String n2 = scanner.nextLine();
System.out.println("Team 2: Enter a Location.");
String l2 = scanner.nextLine();
System.out.println("Team 2: Enter an Offensive Rating (0-100).");
String o2 = scanner.nextLine();
System.out.println("Team 2: Enter a Defensive Rating (0-100).");
String d2 = scanner.nextLine();
Team team2 = new Team(n2,l2,o2,d2);
competition(team1,team2);
}
}
我已经检查了课堂讨论区,甚至查了解如何解决这个问题,但没有任何东西可以帮助我!提前谢谢!
答案 0 :(得分:2)
你有一个构造函数需要2个字符串和2个整数,但你试图传递4个字符串。将最后两个字符串转换为int
,然后再将它们传递给构造函数。
对于competition
,声明时拼写错误。变化
public static int competion(Team home, Team away){
到
public static int competition(Team home, Team away){
答案 1 :(得分:0)
您的构造函数定义为
public Team(String n, String loc, int o, int d)
请注意,构造函数接受两个String类型的参数和两个类型为int的参数。
您应该在main方法中使用以下代码。
System.out.println("Team 1: Enter an Offensive Rating (0-100).");
int o1 = scanner.nextInt();
System.out.println("Team 1: Enter a Defensive Rating (0-100).");
int o2 = scanner.nextInt();
始终使用THIS作为参考。