addScore方法是重写当前数组值 而不是附加到当前列表

我是一个新手 java 学生。我有一个 java 类的项目,我在那里写一个 Golfer 类,Score 类和一个 Golfer 类的测试人员来测试所有方法。我遇到的具体问题是:

我是一个新手 java 学生。我有一个 java 类的项目,我在那里写一个 Golfer 类,Score 类和一个 Golfer 类的测试人员来测试所有方法。我遇到的具体问题是:

当我调用 addScore 方法时,该方覆盖旧数据,而不是添加到现有的。我需要让程序在之前的分数之外添加数组中的分数。

findScore 方法是私有的,并在公共方法 getScore 中使用,但是,当我运行程序时,无论调用中的参数如何,我都会得到 null 值。我需要根据输入的日期返回数组的索引。

以下是代码摘录,而不是整个程序。

public class Golfer {
   /**String representing the golfer's name*/
   private String name;
   /**String representing the golf course where the golfer's hadicap is kept*/
   private String homeCourse;
   /**Unique integer that identifies every golfer*/
   private int idNum;
   /**Array storing all the golfer's scores*/
   private Score[] scores;
   public static int nextIDNum;
   /**Default constructor, sets all instance field to a default value.     Creates Array.
   */
   public Golfer() {
      name = "";
      homeCourse = "";
      scores = new Score[0];
      nextIDNum = 1000;
   }
   /**Constructor sets name and homeCourse from parameters and uses the static variable nextIDNum to retrieve the next available ID number. Creates Array.
   */
   public Golfer(String golferName, String home) {
      setName(golferName);
      setHomeCourse(home);
      setNextIDNum(nextIDNum);
      scores = new Score[10];
   }
   /**Creates a Score object from the parameters that represent the course, course rating, course slope, date and score.  Adds the newly created Score object to the Array of Scores. 
   @param golfCourse A String representing the golf course name
   @param rating A double representing the golf course rating
   @param slope An int representing the golf course slope
   @param scoreDate A String representing the date the course was played
   @param score An int representing what was scored on the course
   */
   public void addScore(String golfCourse, double rating, int slope, String scoreDate, int score) {
      Score[] golfScores = new Score[scores.length + 1];
      for (int i = 0; i < scores.length; i++) {
             golfScores[i] = scores[i]; 
      }
      golfScores[golfScores.length - 1] = new Score(golfCourse, rating, slope, scoreDate, score);
      scores = golfScores;
   }
   /**Deletes a score from the Array based on score date,  Assumes only one score per day. 
   @param golfDate A string representing the date of the golf score
   @return true if score found and deleted, 
   @return false if score not found.
   */
   public boolean deleteScore (String golfDate) {
      for (int i = 0; i < scores.length; i++) {
         if (findScore(golfDate) > 0) {
            scores[i] = null;
         } 
         return true;        
      }
      return false;
   }
   /**Returns a score object based on the score date. If not found returns null
   @param golfDate The date of the golf score
   @return Scores[i] The score on the parameterized date
   @return null A null value if the score was not found.
   */
   public Score getScore(String golfDate) {
      for (int i = 0; i < scores.length; i++) {
         if (findScore(golfDate) > 0) {
            return scores[i];
            }  
      }
      return null;
   }
   /**Given a parameter representing the score's date, finds the score    on a given date and returns the Array index of a score. Return constant NOTFOUND if not found.
   @param golfDate A string representing the date of the score
   @return i An array index representing the score
   @return NOTFOUND A constant set to -1 if the score isn't found
   */
   private int findScore(String golfDate) {
      final int NOTFOUND = -1;
      for (int i = 0; i < scores.length; i++) {
         if (scores[i].equals(golfDate)) {
            return i;
         }
      }
      return NOTFOUND;
   }
}

分数类:

public class Score {
   private String courseName;
   private int score;
   private String date;
   private double courseRating;
   private int courseSlope;
   public Score(String course, double rating, int slope, String golfDate, int scr) {
      setCourseName(course);
      setScore(scr);
      setDate(golfDate);
      setCourseRating(rating);
      setCourseSlope(slope);
   }
   public Score() {
      courseName = "";
      score = 0;
      date = "";
      courseRating = 0.0;
      courseSlope = 0;
   }
   public void setCourseName(String course) {
      courseName = course;
   }
   public String getCourseName() {
      return courseName;
   }
   public void setScore(int golfScore) {
      if ((golfScore < 40) && (golfScore > 200)) {
         golfScore = 9999;
         System.out.println("Error: golf score must be between 40 and 200.");
         }
            score = golfScore;
   }
   public int getScore() {
      return score;
   }
   public void setDate(String golfDate) {
      date = golfDate;
   }
   public String getDate() {
      return date;
   }
   public void setCourseRating(double rating) {
      if ((rating < 60) && (rating > 80)) {
         rating = 9999;
         System.out.println("Error: the course rating must be between 60 and 80.");
         }
            courseRating = rating;
   }
   public double getCourseRating() {
      return courseRating;
   }
   public void setCourseSlope(int slope) {
      if ((slope < 55) && (slope > 155)) {
         slope = 9999;
         System.out.println("Error: The course slope must be between 55 and 155.");
         }
            courseSlope = slope;
   }
}

测试仪类:

public class GolferTester {
   public static void main (String []args) {
      String course1 = "Augusta National";
      String course2 = "Bayhill CC";
      String course3 = "TPC Sawgrass";
      String player1 = "Sam Snead";
      String player2 = "Arnold Palmer";
      String player3 = "Jack Nicklaus";
      int score1  = 66;
      int score2  = 201;
      int score3  = 72;
      int slope1  = 60;
      int slope2  = 156;
      int slope3  = 77;
      double rating1 = 65.2;
      double rating2 = 81.8;
      double rating3 = 70.9;
      String date1 = "01/01/2017";
      String date2 = "06/01/2016";
      String date3 = "12/22/2016";
      Golfer golfer1    = new Golfer(player1, course1);
      Golfer golfer2    = new Golfer(player2, course2);
      Score s1 = new Score(course1, rating1, slope1, date1, score1);
      Score s2 = new Score(course2, rating2, slope2, date2, score2);
      s1.setScore(score1);
      s1.setDate(date1);
      s1.setCourseRating(rating1);
      s1.setCourseSlope(slope1);
      s1.setCourseName(course1);
      s2.setScore(score3);
      s2.setDate(date3);
      s2.setCourseRating(rating3);
      s2.setCourseSlope(slope3);
      s2.setCourseName(course3);
      golfer1.addScore(s1.getCourseName(), s1.getCourseRating(), s1.getCourseSlope(), s1.getDate(), s1.getScore());
      golfer2.addScore(s2.getCourseName(), s2.getCourseRating(), s2.getCourseSlope(), s2.getDate(), s2.getScore());
      System.out.println(golfer1);
      System.out.println("");      
      System.out.println(golfer2);
      System.out.println("");
      s1.setScore(score2);
      s1.setCourseRating(rating2);
      s1.setCourseSlope(slope2);
      golfer1.addScore(s1.getCourseName(), s1.getCourseRating(), s1.getCourseSlope(), s1.getDate(), s1.getScore());     
      System.out.println(golfer1);
      deleteScore(s1.getDate());
      System.out.println(s1.getDate());
   }
}

任何帮助都将被吸收

0

不要将数组用于您知道会增长 / 收缩的元素列表。使用 ArrayList,它具有 add()

您的 getScore 方法总是返回 null

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(128)
找不到模块:无法解析“@ aws-amplify/core”
上一篇
如何计算多核 CPU的循环等待时间
下一篇

相关推荐

  • vscode code命令:# 如何使用 VSCode 命令

    VSCode Code命令是Visual Studio Code的一个内置命令,用于在本地和远程服务器上执行代码。它可以让你在不同的系统上运行代码,而不需要安装任何软件。…

    2023-01-22 09:55:35
    0 15 22
  • cv糖醋排骨是弯的吗弯曲的美味

    cv糖醋排骨不是弯的,它是一种制作方法,通常用来制作排骨。代码:…

    2023-01-30 08:19:31
    0 36 45
  • cv树洞柒夜事件CV树洞中的秘密与惊奇

    cv树洞柒夜事件是一个有趣的计算机视觉活动,它提供了一种新的方式来利用计算机视觉技术来探索和发现景观。它始于2020年7月,由一群热爱计算机视觉的研究者和开发者发起,他们希望通过使用机器学习技术来探索景观中的精彩之处。cv树洞柒夜事件的目标是使用机器学习技术来探索景观中的精彩之处,并利用这些发现来改善景观设计。参与者需要使用机器学习技术来探索景观,并利用这些发现来改善景观设计。参与者可以使用Python,OpenCV,TensorFlow等技术来完成任务,并将其发布在GitHub上。…

    2023-01-08 12:49:50
    0 49 78
  • cv小随:如何利用CV技术提升求职成功率

    示例示例cv小随是一种基于计算机视觉的智能技术,它可以帮助用户自动识别图像中的物体、场景和行为,从而实现自动化的图像处理。cv小随可以用于多种应用场景,如图像搜索、图像分类、图像识别、图像检测等。…

    2023-01-01 11:12:42
    0 20 68
  • win10可以安装cad2007吗:CAD2007是否可以在Win10上安装?

    不可以,因为CAD2007不支持Windows 10系统。如果想要安装CAD2007,需要安装在Windows XP或者Windows 7系统上。…

    2023-01-20 02:04:42
    0 50 86
  • iban code是什么意思一种国际银行帐户号码标准

    IBAN(International Bank Account Number)代码是一种国际标准的银行账户号码,用于在不同国家之间进行国际转账。它由一串字母和数字组成,通常由2至4个字母开头,后面跟着一串数字。…

    2022-12-30 09:24:47
    0 78 64
  • cv 机器视觉:如何利用机器视觉技术提高图像处理效率

    CV(Computer Vision)机器视觉是一门研究计算机如何理解和处理图像的学科,它旨在使计算机能够以人类般的方式识别和理解图像。它主要应用于图像处理、图像分析、计算机视觉等领域,广泛应用于机器人、自动驾驶、医疗影像分析等领域。…

    2023-01-23 11:32:51
    0 31 64
  • mac 开发c++:Mac 下开发 C++ 的入门指南

    示例示例Mac OS X 是一个强大的操作系统,可以用来开发 C++ 应用程序。下面是一个简单的 C++ 程序的代码示例:#include…

    2023-01-02 10:39:58
    0 55 99

发表评论

登录 后才能评论

评论列表(85条)