[英]I want to be able to compare two manga's ratings but I don't know how to make it so I can input two titles that then grabs the scores to compare
我在一個數組列表中有 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!")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.