【问题标题】:Need a help figuring out some nested iteration things in Java需要帮助找出 Java 中的一些嵌套迭代的东西
【发布时间】:2021-04-05 23:15:03
【问题描述】:

我对编程很陌生,不久前才开始做一些 CS,目前我的任务是编写一个程序,打印字母表中的每个字母以及该字母在短语中出现的次数。它需要包括:

-循环遍历字母表(外循环) -循环遍历短语的所有字母(内部循环) - 计数器变量来计算一个字母在短语中出现的次数 -打印语句以显示频率

我在这方面有点落后,很难抽出时间来完成这项任务,只需要一些关于如何简单地完成这项工作的指导,而不需要像数组之类的复杂东西。

正如我所说,我没有太多时间来做这件事,但这里是总体布局/我认为它应该是什么样子的计划:

public class LetterFrequencies
{
    public static void main(String[] args)
    {
        int counter = 0;
        String letters = "abcdefghijklmnopqrstuvwxyz"; 
        String phrase = "This is a phrase";

        System.out.println("The following shows the letter frequencies for the phrase");

    //for (int i = _; i < _; i++) ??
    {
      //Print out statement like :System.out.println(letters.substing());
    }
      //possible while loop or for loop? 
    }
}

如有必要,请随时纠正我,因为我需要在不久的将来改进的所有知识,谢谢!

【问题讨论】:

  • 很抱歉,您所说的字母频率是什么意思,例如,短语“This is a phrase”的输出是什么。
  • 输出应该是这样的:a 2 b 0 c 0 d 0 e 1 f 0 g 0 h 2 i 2 j 0 ect...
  • 实际上,您需要花时间来解决这个问题,因为除非您自己尝试完成,否则所有的帮助都是无用的。

标签: java nested iteration


【解决方案1】:

试试这个不使用数组或列表的解决方案,也许它会帮助你:

public static void main(String[] args) {
        int counter = 0;
        String letters = "abcdefghijklmnopqrstuvwxyz"; 
        String phrase = "This is a phrase".toLowerCase();
        for (int i = 0; i < letters.length(); i++) {
            int count = 0;
            char a = letters.charAt(i);
            for (int j = 0; j < phrase.length(); j++) {
                if (a == phrase.charAt(j)) {
                    count++;
                }
            }
            System.out.println("The letter " + letters.charAt(i) + " " + count);
        }
        
    }

对于每个字母,您将知道它在您的短语中使用了多少时间。

结果:

The letter a 2
The letter b 0
The letter c 0
The letter d 0
The letter e 1
The letter f 0
The letter g 0
The letter h 2
The letter i 2
The letter j 0
The letter k 0
The letter l 0
The letter m 0
The letter n 0
The letter o 0
The letter p 1
The letter q 0
The letter r 1
The letter s 3
The letter t 1
The letter u 0
The letter v 0
The letter w 0
The letter x 0
The letter y 0
The letter z 0

【讨论】:

  • 感谢您的反馈
【解决方案2】:

这是一种方式:

    String lettersString = "abcdefghijqlmnñopqrstuvwxyz";
    char[] letters = lettersString.toCharArray();
    int[] counts = new int[letters.length];
    Arrays.fill(counts, 0);
    String textBoxValue = "This is the message";
    for (int i = 0; i < textBoxValue.length(); i++) {
        char currentLetter = textBoxValue.charAt(i);
        for (int j = 0; j < letters.length; j++) {
            char letter = letters[j];
            if (currentLetter == letter ||
            currentLetter == Character.toUpperCase(letter)){
                counts[j]++;
            }
        }
    }
    for (int i = 0; i < letters.length; i++) {
        //Print it your own way
        String message = letters[i] + " : " + counts[i];
        Log.d(TAG, message);
        System.out.println(message);
    }

【讨论】:

    【解决方案3】:

    这可能会有所帮助,只是尝试做一个频率图。

        public static Map<Character, Integer> frequencies(String yourPrhase){
            Map<Character, Integer> frequency = new HashMap<>();
            for(final char c : str.toCharArray()){
                frequency.compute(c, (k, v) -> v == null ? 1: v++);
            }
            return frequency;
    }
    

    然后你可以用频率图做你需要的,可能打印出来。

    也可以试试这个功能更强大的。

       public static Map<String, Long> frequencies2(String yourPrhase){
        return  str.chars()
            .mapToObj(c -> String.valueOf((char) c))
            .collect(Collectors.groupingBy(String::valueOf, Collectors.counting()));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 2011-02-24
      相关资源
      最近更新 更多