本文介绍了如何计算的次数的词出现在阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想算的次数每个单词是Java中的数组中,然后显示它,但我想不出我如何使用扫描仪添加到数组,然后试图找到一种方法这将通过阵列,并显示每个字有多少次是该数组中
公共类计数{ 静态的String []字=新的String [3]; //静态INT [] aCounts; 私有静态诠释计数; 公共静态无效countTimesWordApperesInArray(){ INT大小= words.length; 的for(int i = 0; I<大小;我++){ INT位置=我; 诠释计数= 0; 对于(INT J = 0; J<大小; J ++){ 字符串元素=字[我] 如果(字[I]。载(元素)){ 算上++; } } 的System.out.println(字[I] ++计数); } } 公共静态无效的主要(字串[] args){ 的System.out.println(输入三个词); 扫描仪扫描=新的扫描仪(System.in); 字符串输入= scanner.next(); 而((! - 1)等于(输入)){ 字[计数] =输入; 算上++; 输入= scanner.next(); } //打印(); countDigits(); }}解决方案
改变字[I]。载(元素)到字[J ] .equals(元素)
公共静态无效countTimesWordApperesInArray(){ INT大小= words.length; 的for(int i = 0; I<大小;我++){ INT位置=我; 诠释计数= 0; 字符串元素=字[我] 对于(INT J = 0; J<大小; J ++){ 如果(字[J] .equals(元素)){ 算上++; } } 的System.out.println(字[I] ++计数); } }I am trying to count the number of times each word is in the array in java and then display it, but I can't figure out how I am using a scanner to add to the array and then trying to find a method that will go through the array and show how many times each word is in that array.
public class Counting { static String[] words = new String[3]; //static int[] aCounts; private static int count; public static void countTimesWordApperesInArray() { int size = words.length; for (int i = 0; i < size; i++) { int position = i; int count = 0; for (int j = 0; j < size; j++) { String element = words[i]; if (words[i].contains(element)) { count++; } } System.out.println(words[i] + " " + count); } } public static void main(String[] args) { System.out.println("Enter three Words"); Scanner scanner = new Scanner(System.in); String input = scanner.next(); while (!("-1").equals(input)) { words[count] = input; count++; input = scanner.next(); } //print(); countDigits(); } }解决方案
change words[i].contains(element) to words[j].equals(element)
public static void countTimesWordApperesInArray() { int size = words.length; for (int i = 0; i < size; i++) { int position = i; int count = 0; String element = words[i]; for (int j = 0; j < size; j++) { if (words[j].equals(element)) { count++; } } System.out.println(words[i] + " " + count); } }
更多推荐
如何计算的次数的词出现在阵列
发布评论