gpt4 book ai didi

Java:单元测试未返回正确的结果

转载 作者:行者123 更新时间:2023-11-30 07:03:51 25 4
gpt4 key购买 nike

我正在编写一个程序,如果游戏没有任何评论,那么我需要将其添加到 map 中。如果游戏有评论,则将给定的评论添加到相应的 GameInfo 中。我的代码编译得很好,但我的单元测试没有返回我需要的点,以便它们返回以表明我的代码正在正确执行。这是我的代码:

class GameInfoCollection {
// TODO - you need to use a Map (from a String, the title, to a GameInfo) to keep track of all the GameInfo's
Map<String, Integer> titles = new HashMap<String, Integer>();
// TODO - if there are no reviews for the game, create a new GameInfo (with this review) and add it to the map
// if there's one, add the given review to the corresponding GameInfo
public void addGameReview(String gameTitle, Review r)
{
if (titles.isEmpty()) {
GameInfo g = new GameInfo("Review");
} else titles.put(gameTitle, 1);

}

public int getNumberOfReviewsForGame(String gameTitle)
{
// TODO - implement this
return titles.get(titles);
}

这是我对这些方法的单元测试。它应该返回 20 分,但没有返回任何分:

public void testGetNumberOfReviewsForGame()
{
GameInfoCollection gic=new GameInfoCollection();


gic.addGameReview("g1",new Review("cool",5));
gic.addGameReview("g1",new Review("cool",3));
gic.addGameReview("g2",new Review("cool",2));
gic.addGameReview("g3",new Review("cool",2));

Assert.assertEquals(2,gic.getNumberOfReviewsForGame("g1"));
Assert.assertEquals(1,gic.getNumberOfReviewsForGame("g2"));
gic.addGameReview("g1",new Review("cool",3));
Assert.assertEquals(3,gic.getNumberOfReviewsForGame("g1"));
}

@Grade(points=20)
@Test

这是我的整个程序的代码。请注意,最后有一些部分我还没有完成。

package assignment;

import java.text.MessageFormat;
import java.util.Scanner;

import java.awt.Point;
import java.awt.Dimension;
import java.awt.Rectangle;

import java.time.LocalDate;

import java.util.*; // List, ArrayList, Map, HashMap

class Review {
public String reviewText;
public int numberOfStars;

public Review(String reviewText, int numberOfStars) {
this.reviewText=reviewText;
this.numberOfStars=numberOfStars;
}
}

class GameInfo {
private String title;
// need an ArrayList to keep the reviews;
private Review[] reviews = new Review[10];
int numReviews=0;

public GameInfo(String title) {
this.title=title;
// you may want to initialize any other variables you create here
}

public String getTitle() {
return title;
}

// TODO - adds the review to the 'array' of reviews. You need to keep all reviews in an array
public void addReview(Review r) {
reviews[numReviews] = r;
++numReviews;
}

// TODO - returns the number of reviews which have been added to this GameInfo
public int getNumberOfReviews() {
return numReviews;
}

// TODO - returns the sum of the number of stars which have been added to this GameInfo
// you have to calculate this from your array
public int getSumOfStars() {
int sum=0;
for (int i=0; i<numReviews;++i)
sum +=reviews[i].numberOfStars;
return sum;
}

// TODO - returns the average number of stars for this GameInfo's reviews
// again, have to calculate this (or at least the sum of stars) from your array
public double getAverageStarRating() {
double firstNumber = getSumOfStars();
double secondNumber = getNumberOfReviews();
double avg = firstNumber/secondNumber;

return avg;
}
}

// TODO - you need to implement all these methods
class GameInfoCollection {
// TODO - you need to use a Map (from a String, the title, to a GameInfo) to keep track of all the GameInfo's
Map<String, Integer> titles = new HashMap<String, Integer>();
// TODO - if there are no reviews for the game, create a new GameInfo (with this review) and add it to the map
// if there's one, add the given review to the corresponding GameInfo
public void addGameReview(String gameTitle, Review r)
{
if (titles.isEmpty()) {
GameInfo g = new GameInfo("Review");
} else titles.put(gameTitle, 1);

}

public int getNumberOfReviewsForGame(String gameTitle)
{
// TODO - implement this
return titles.get(titles);
}

请注意,我对 Java 还很陌生,非常感谢您的帮助。谢谢!

最佳答案

这看起来像是一项家庭作业,我猜单元测试是您老师提供的代码的一部分。单元测试失败的事实并不意味着单元测试是错误的,而是意味着您的代码行为不正确。特别是GameInfoCollection中的部分你还没有完成。

那么,让我们看一下每个 TODO 注释的含义:

// TODO - you need to use a Map (from a String, the title, to a GameInfo) to keep track of all the GameInfo's

Map 具有键和值,据说是从键映射到值。在声明中Map<String, Integer> ,第一个类型(String)是键的类型,第二个是值的类型。这里的 TODO 是说值的类型需要是 GameInfo .

// TODO - if there are no reviews for the game, create a new GameInfo  (with this review) and add it to the map

这一切的每个部分都需要在 if 内完成分支。您需要 a) 创建一个新的 GameInfo ,b) 将评论放入其中,c) 将其添加到 map 中。您当前只执行 a) 部分,即使如此,您也没有将正确的值传递给构造函数 - 看看 GameInfo构造函数及其所采用的参数名称; “评论”与此相符吗?

// if there's one, add the given review to the corresponding GameInfo 

这是关于 else 中应该包含的内容。分支。

// TODO - implement this

从技术上讲,此方法已经有一个实现,但您需要对其进行更改以正确匹配第一个 TODO 中关于 map 值类型的更改。

关于Java:单元测试未返回正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40454502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com