如何从数组中获取最多出现的单词 [英] How can I get the word with the max occurrences from an array

查看:84
本文介绍了如何从数组中获取最多出现的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以建议我在findBestSeller()方法中进行哪些更改以获得所需的输出?我的2个输出是正确的,但3个是错误的5.提前感谢。



Can anyone suggest me that what changes do I have to make in findBestSeller() method to get the desired output? My 2 outputs are correct but 3 are wrong out of 5. Thanks in advance.

          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(".");
      }
	}
    }  





我尝试了什么:



//期望输出





What I have tried:

//Desired Output

  ----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.





//我的输出





// My Output

    ----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.

推荐答案

你的测试#2失败,因为这个循环

You fail test #2 because this loop
for (int j = 1; j < items.length; j++)



从第二个字开始计算。



你应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。


start counting at second word.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


我不确定下面的代码应该做什么,但它对我来说有点怀疑。

I am not sure what the following code is supposed to do, but it looks a little suspect to me.
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++;
 }



对于数组中的每个元素,您将其与所有其他元素进行比较。但是你每次都进行完整的数组比较,以便重复进行大量的比较,这表明在某些情况下你会得到无效的结果。



也许它应该实际上是这样的:


For each element in the array you compare it with all the others. But you do the full array compare each time so that a lot of the comparisons will be repeated, suggesting that you will get invalid results in some cases.

Maybe it should really be something like:

for (int i = 0; i < (items.length - 1); i++)
{
 temp = items[i];
 tempCount = 0;
 for (int j = i; j < items.length; j++) // j should start at the next item after temp.
 {
  if (temp == items[j])
   tempCount++;
 }



你也在每次外循环重置 tempcount 所以当这完成时,它很可能没有你想要的值。


You are also resetting tempcount for each time round the outer loop so when this finishes it will most likely not have the value you are looking for.


这篇关于如何从数组中获取最多出现的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆