有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java编译错误已尝试修复,但没有成功

我在一个入门级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);
   }
 }

我查看了课堂讨论板,甚至查看了如何解决这个问题,但没有任何东西能帮到我!提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    有一个构造函数接受2个字符串和2个整数,但试图传递4个字符串。在将最后两个字符串传递给构造函数之前,将其转换为int

    对于competition,它在声明时拼写错误。改变

    public static int competion(Team home, Team away){
    

    public static int competition(Team home, Team away){
    
  2. # 2 楼答案

    您的构造函数定义为

    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作为参考