Как я могу получить слово с максимальным количеством вхождений из массива
Может ли кто-нибудь подсказать мне, какие изменения я должен внести в метод findBestSeller (), чтобы получить желаемый результат? Мои 2 вывода верны, но 3 неверны из 5. Заранее спасибо.
public class QuizIIC { public static String findBestSeller(String[] items) { int count = 1, tempCount; String popular = items[0]; String temp = "0"; for (int i = 0; i < (items.length - 1); i++) { temp = items[i]; tempCount = 0; for (int j = 1; j < items.length; j++) { if (temp == items[j]) tempCount++; } if (tempCount > count) { popular = temp; count = tempCount; } } return popular; } //Boolean runTest Method private static boolean runTest(int testNum, String[] p0, boolean hasAnswer, String p1) { System.out.print("Test #" + testNum + ": [" + "{"); for (int i = 0; p0.length > i; ++i) { if (i > 0) { System.out.print(","); } System.out.print("\"" + p0[i] + "\""); } System.out.print("}"); System.out.println("]"); String answer; answer = QuizIIC.findBestSeller(p0); boolean res; res = true; if (hasAnswer) { System.out.println("Desired answer:"); System.out.println("\t" + "\"" + p1 + "\""); } System.out.println("Your answer:"); System.out.println("\t" + "\"" + answer + "\""); if (hasAnswer) { res = answer.equals(p1); } if (!res) { System.out.println("Sorry!"); } else if (hasAnswer) { System.out.println("Correct!"); } System.out.println(); return res; } //Main Method public static void main(String[] args) { String[] p0; String p1; int count = 0; // ----- test 1 ----- p0 = new String[]{"table","chair","table","table","lamp","door","lamp","table","chair"}; p1 = "table"; if( runTest(1, p0, true, p1) ) count++; // ------------------ // ----- test 2 ----- p0 = new String[]{"a","a","a","b","b","b"}; p1 = "a"; if( runTest(2, p0, true, p1) ) count++; // ------------------ // ----- test 3 ----- p0 = new String[]{"icecream","peanuts","peanuts","chocolate","candy","chocolate","icecream","apple"}; p1 = "chocolate"; if( runTest(3, p0, true, p1) ) count++; // ------------------ // ----- test 4 ----- p0 = new String[]{"soul"}; p1 = "soul"; if( runTest(4, p0, true, p1) ) count++; // ------------------ // ----- test 5 ----- p0 = new String[]{"a","b","b","b","c","c","c","c","a","a","a","a","b","b"}; p1 = "a"; if( runTest(5, p0, true, p1) ) count++; // ------------------ System.out.print( count + " out of 5" ); if (count == 5) { System.out.println("!"); } else { System.out.println("."); } } }
Что я уже пробовал:
//желаемый результат
----jGRASP exec: java QuizIIC -Xlint:unchecked Test #1: [{"table","chair","table","table","lamp","door","lamp","table","chair"}] Desired answer: "table" Your answer: "table" Correct! Test #2: [{"a","a","a","b","b","b"}] Desired answer: "a" Your answer: "a" Correct! Test #3: [{"icecream","peanuts","peanuts","chocolate","candy","chocolate","icecream","apple"}] Desired answer: "chocolate" Your answer: "chocolate" Correct! Test #4: [{"soul"}] Desired answer: "soul" Your answer: "soul" Correct! Test #5: [{"a","b","b","b","c","c","c","c","a","a","a","a","b","b"}] Desired answer: "a" Your answer: "a" Correct! 5 out of 5! ----jGRASP: operation complete.
// Мой Вывод
----jGRASP exec: java QuizIIC Test #1: [{"table","chair","table","table","lamp","door","lamp","table","chair"}] Desired answer: "table" Your answer: "table" Correct! Test #2: [{"a","a","a","b","b","b"}] Desired answer: "a" Your answer: "b" Sorry! Test #3: [{"icecream","peanuts","peanuts","chocolate","candy","chocolate","icecream","app le"}] Desired answer: "chocolate" Your answer: "peanuts" Sorry! Test #4: [{"soul"}] Desired answer: "soul" Your answer: "soul" Correct! Test #5: [{"a","b","b","b","c","c","c","c","a","a","a","a","b","b"}] Desired answer: "a" Your answer: "b" Sorry! 2 out of 5. ----jGRASP: operation complete.
Patrice T
Каково правило выбора победителя, когда 2 или более имеют максимальное количество очков ?
[no name]
И причина, по которой вы не можете отладить это самостоятельно, заключается в том, что...?