我希望能够比较两个漫画的评分,但我不知道如何制作,所以我可以输入两个标题,然后抓取分数进行比较



我在数组列表中设置了10部不同漫画的数据,它们分别是标题、评级、是否进行以及章节数量。我希望能够将两个不同的漫画标题输入扫描仪,然后让它比较两者的评分,看看哪个更高。这是我目前掌握的代码。感谢您提前提供的帮助。

public class TopMangaData {
private String title;
private double rating;
private boolean onGoing;
private int chapters;
public TopMangaData(String title, double rating, boolean onGoing, int chapters) {
this.title = title;
this.rating = rating;
this.onGoing = onGoing;
this.chapters = chapters;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getRating() {
return rating;
}
public void setRating(double rating) {
this.rating = rating;
}
public boolean getOnGoing() {
return onGoing;
}
public void setOnGoing(boolean onGoing) {
this.onGoing = onGoing;
}
public int getChapters() {
return chapters;
}
public void setChapters(int chapters) {
this.chapters = chapters;
}
public String toString() {
return "nTop Manga Data nTitle: " + title + "nRating: " + rating + "nOn going: " + onGoing + "nChapters: " + chapters;
}

}

import java.util.ArrayList;

导入java.util.Scanner;

公共类TopMangaDataRunner{public static void main(String[]args({

ArrayList<TopMangaData> TopMangaData = new ArrayList<TopMangaData>(10); {
TopMangaData.add(new TopMangaData("Berserk", 9.43, false, 380));
TopMangaData.add(new TopMangaData("JoJo's Bizarre Adventure Part 7: Steel Ball Run", 9.27, false, 96));
TopMangaData.add(new TopMangaData("One Piece", 9.17, true, 1041));
TopMangaData.add(new TopMangaData("Vagabond", 9.16, false, 327));
TopMangaData.add(new TopMangaData("Monster", 9.12, false, 162));
TopMangaData.add(new TopMangaData("Fullmetal Alchemist", 9.07, false, 116));
TopMangaData.add(new TopMangaData("Grand Blue", 9.06, true, 75));
TopMangaData.add(new TopMangaData("Goodnight Punpun", 9.05, false, 147));
TopMangaData.add(new TopMangaData("Slam Dunk", 9.04, false, 276));
TopMangaData.add(new TopMangaData("Vinland Saga", 8.99, true, 190));

for(TopMangaData m :TopMangaData) {
System.out.println(m.toString());
}
Scanner scan = new Scanner(System.in);

String firstComparison;
String secondComparison;

System.out.println("");
System.out.println("Want to compare two of these top manga's scores?");
System.out.println("Input the first manga you would like to compare:");

firstComparison = scan.nextLine();

System.out.println("Input the second manga you would like to compare:");

secondComparison = scan.nextLine();



}

}
}

首先,您需要从列表中找到要比较的对象。最简单的方法是迭代列表并检查标题(假设标题在列表中(:

int firstComparisonValue = 0;
for(TopMangaData m :TopMangaData)
if(m.getTitle().equals(firstComparison))
firstComparisonValue = m.getRating();
int secondComparisonValue = 0;
for(TopMangaData m :TopMangaData)
if(m.getTitle().equals(secondComparison))
secondComparisonValue = m.getRating();

然后只需比较您的值并打印出大型的标题

if(firstComparisonValue > secondComparisonValue){
System.out.println("First manga has higher rating")
}
else if (firstComparisonValue < secondComparisonValue){
System.out.println("Second manga has higher rating")
}
else {
System.out.println("Both have the same rating!")
}