简体   繁体   English

扫描数组以查找最常见的字长的方法,但如果两个不同的元素具有相同的长度,则会发送错误消息

[英]Method that scans array for most common word length, but sends error message if two different elements have the same length

I have a method that takes in an array (created by Scanner input in main) and sorts the array to find the most common words before printing the length of those words.我有一个方法,它接收一个数组(由扫描仪在 main 中的输入创建)并对数组进行排序以在打印这些单词的长度之前找到最常见的单词。 However, I'm also trying to have an error message pop up if two different elements have the same length;但是,如果两个不同的元素具有相同的长度,我也会尝试弹出错误消息; this is where I'm having trouble getting started.这是我开始遇到困难的地方。

Method:方法:

//Return the length of the most common words
public static int lengthOfWord(String[] arr){
  
  int arrLength = arr.length;
  int count = 1; 
  int maxCount = 1;
  String temp = arr[0]; 
  
  //Sort through array
  for (int i = 1; i < arr.length; i++)  { 
     if (arr[i] == arr[i - 1]) {
        count++;
     } else {
     if (count > maxCount) { 
        maxCount = count; 
        temp = arr[i - 1];
     }//End of conditional
     count = 1;
     }//End of conditional
  }// End of for loop 

  //Declare most frequent length
  if (count > maxCount) { 
     maxCount = count; 
     temp = arr[arrLength - 1];
  }//End of conditional

  return temp.length();

}//End of lengthOfWord
  1. String comparison - arr[i] == arr[i-1]<\/code> does not work.字符串比较 - arr[i] == arr[i-1]<\/code>不起作用。 Each string object is different for Java.对于 Java,每个字符串对象都是不同的。 Use arr[i].equals(arr[i-1])<\/code> .使用arr[i].equals(arr[i-1])<\/code> 。<\/li>
  2. When you check count > maxCount<\/code> , it means there is definitely a string more frequent than the previous one.当您检查count > maxCount<\/code>时,这意味着肯定有一个字符串比前一个字符串更频繁。 However, what if count == maxCount<\/code> ?但是,如果count == maxCount<\/code>怎么办? - It would mean you have found two strings with equal frequency. - 这意味着你找到了两个频率相等的字符串。<\/li><\/ol>

    Use a flag that tells you if there are multiple max frequency strings or not.使用一个标志来告诉您是否有多个最大频率字符串。 So now we are going to check both for greater and equal conditions and based on what is true, we are going to set a flag unique<\/code> .所以现在我们要检查更大和相等的条件,并根据什么是真的,我们将设置一个标志unique<\/code> 。

     \/\/Return the length of the most common words public static int lengthOfWord(String[] arr){ int arrLength = arr.length; int count = 1; int maxCount = 1; boolean unique = true; String temp = arr[0]; \/\/Sort through array for (int i = 1; i < arr.length; i++) { if (arr[i] == arr[i - 1]) { count++; } else { if (count > maxCount) { unique = true; maxCount = count; temp = arr[i - 1]; } else if(count == maxCount) { unique = false; } \/\/End of conditional count = 1; }\/\/End of conditional }\/\/ End of for loop \/\/Declare most frequent length if (count > maxCount) { unique = true; maxCount = count; temp = arr[i - 1]; } else if(count == maxCount) { unique = false; }\/\/End of conditional if(!unique) { System.out.println("ERROR"); \/\/ or whatever you want to do in this condition } return temp.length(): ; }\/\/End<\/code><\/pre>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM