Java:单元测试没有返回正确的结果
我正在写一个程序,如果游戏没有任何评论,那么我需要把它添加到地图上。如果有对游戏的评论,则将给定的评论添加到相应的GameInfo中。我的代码编译得很好,但是我的单元测试没有返回我需要返回的点来显示我的代码正确执行。这是我的密码:Java:单元测试没有返回正确的结果,java,unit-testing,Java,Unit Testing,我正在写一个程序,如果游戏没有任何评论,那么我需要把它添加到地图上。如果有对游戏的评论,则将给定的评论添加到相应的GameInfo中。我的代码编译得很好,但是我的单元测试没有返回我需要返回的点来显示我的代码正确执行。这是我的密码: class GameInfoCollection { // TODO - you need to use a Map (from a String, the title, to a GameInfo) to keep track of all the Game
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);
}
这是我整个程序的代码。请注意,最后还有一些部分我还没有完成
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.text.MessageFormat;
导入java.util.Scanner;
导入java.awt.Point;
导入java.awt.Dimension;
导入java.awt.Rectangle;
导入java.time.LocalDate;
导入java.util.*;//列表,数组列表,映射,哈希映射
课堂复习{
公共字符串reviewText;
公众国际明星;
公共评论(字符串评论文本,整数){
this.reviewText=reviewText;
这个.numberOfStars=numberOfStars;
}
}
类游戏信息{
私有字符串标题;
//需要一个ArrayList来保存评论;
私人审查[]审查=新审查[10];
int numReviews=0;
公共游戏信息(字符串标题){
这个.title=title;
//您可能需要初始化在此处创建的任何其他变量
}
公共字符串getTitle(){
返回标题;
}
//TODO-将审阅添加到审阅的“数组”中。您需要将所有审阅保留在一个数组中
公开审查(审查r){
评论[numReviews]=r;
++numReviews;
}
//TODO-返回已添加到此GameInfo的评论数
public int getNumberOfReviews(){
返回numReviews;
}
//TODO-返回已添加到此GameInfo的星星数的总和
//您必须从数组中计算此值
公共int getSumOfStars(){
整数和=0;
对于(int i=0;i这看起来像是一个家庭作业,我猜单元测试是你老师提供的代码的一部分。单元测试失败的事实并不意味着单元测试是错误的,而是你的代码表现不正确。特别是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
中,第一种类型(字符串)是键的类型,第二种类型是值的类型。这里的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
分支内完成。您需要创建一个新的GameInfo
,b)将评论放入其中,c)将其添加到映射中。您当前只执行了a)部分,即使您没有将正确的值传递给构造函数-查看GameInfo
构造函数及其参数的名称;“review”与此匹配吗
// if there's one, add the given review to the corresponding GameInfo
这是关于else
分支中应该包含的内容
// TODO - implement this
从技术上讲,此方法已经有了一个实现,但是您需要对其进行更改,以正确匹配从第一个TODO到map值类型的更改。首先,HashMap是key->value collection(单关键点到单值),似乎您需要多个值的键或类似的东西,可能是一个Multimap(请参阅Guava)或地图。请在您的问题中只包含相关(但完整)代码。@nbrooks我将问题编辑为只包含相关代码code@AlexC谢谢,请问番石榴是什么?//TODO-如果游戏没有评论,请创建一个新的GameInfo(使用此评论)并将其添加到地图“你没有做后者……为什么?
// TODO - implement this