【问题标题】:counter doesn't correctly counter for both uppercase char and lowercase char计数器不能正确计数大写字符和小写字符
【发布时间】:2019-02-07 04:32:47
【问题描述】:

编写一个程序,该程序使用 Scanner 对象首先读入一个表示字符串数组大小 (n) 的数字。创建一个使用第一个数字作为大小的字符串数组。然后,您将在同一行读取 (n) 个城市并将它们存储在数组中。最后,您将读取一个字符(与城市相同的行)。然后程序将遍历城市数组并在屏幕上打印字符在城市中的次数。确保处理大小写 (A != a)。

import java.util.Arrays;
import java.util.Scanner;
public class Problem1 {

    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        String myarray[] = new String[length];
        char ch;
        
        
        for (int i = 0; i < myarray.length; i++) {
            myarray[i]=scanner.next();
         }
    
    ch= scanner.next().charAt(0);
    char ch1= Character.toUpperCase(ch);
    char ch2= Character.toLowerCase(ch);
    for(int y=0; y<myarray.length; y++){
      int counter=0;
      for(int z=0; z<myarray.length; z++){
        
        if(myarray[y].charAt(z)==ch1){
          counter++;
        }else if(myarray[y].charAt(z)==ch2){
          counter++;
        }
      }
      System.out.print(counter+" ");
    }
  }
}

示例输入:

4

多伦多哈利法克斯特鲁罗渥太华 t

示例输出:

2 0 1 2

我的输出:

1 0 1 2

【问题讨论】:

  • 您能否更新您的问题以包含有关您的程序到底出了什么问题的信息,例如。预期输出与实际输出?
  • 我认为您的 for 循环与 z 不太正确,因为 z 需要运行字符串的长度,而不是字符串的数量。即z &lt; myarray[y].length()不是z &lt; myarray.length;

标签: java


【解决方案1】:

要计算所需字符忽略大小写的数量,您可以将Character.toLowerCase() 用于字符串中的字符和所需字符。当您执行str.toLowerCase() 时也是如此,但没有创建新的字符串。

对于这个计数任务,您不需要修改字符串(如str.replaceAll())。一般来说,您的方法很好,但您的 for 循环中有一些错误。

public class Problem {

    public static void main(String... args) {
        try (Scanner scan = new Scanner(System.in)) {
            int[] count = count(readCities(scan), readChar(scan));
            System.out.println(Arrays.stream(count)
                                     .mapToObj(Integer::toString)
                                     .collect(Collectors.joining(" ")));
        }
    }

    private static String[] readCities(Scanner scan) {
        String[] cities = new String[scan.nextInt()];

        for (int i = 0; i < cities.length; i++)
            cities[i] = scan.next();

        return cities;
    }

    private static char readChar(Scanner scan) {
        return scan.next().charAt(0);
    }

    private static int[] count(String[] cities, char ch) {
        int[] count = new int[cities.length];

        for (int i = 0; i < cities.length; i++)
            for (int j = 0; j < cities[i].length(); j++)
                if (Character.toLowerCase(cities[i].charAt(j)) == Character.toLowerCase(ch))
                    count[i]++;

        return count;
    }
}

【讨论】:

  • 我真的怀疑他是否明白这一点。这不是比他理解的更高吗?
【解决方案2】:

for(int z=0; z&lt;myarray.length; z++){

把上面提到的for循环改成如下

for(int z=0; z&lt;myarray[y].length(); z++){

myarray.length 将表示输入字符串的总数(4),即循环将迭代直到第 4 个字符或字符串,因此第一个字符串是“Toronto”并且循环将迭代直到第二次出现“o”剩余不会迭代。

myarray[y].length() 将表示字符串中的字符总数。所以现在循环将迭代一个字符串的所有字符。

【讨论】:

    【解决方案3】:

    首先,通常最好使用 // 和 /* 文本 */ 进行注释

    其次,你的计数器不起作用,因为你只计算数组的长度而不是字符串的长度,而是使用 'String.replaceAll(String, String);'和'String.length()'你可以数数。

    会给你的字符计数器一个错误。

    输入:2 A B t

    使用变量

    • 长度(整数)

    • myarray (String[])

    • ch (char)

    int[] countChar(int length, String[] myarray, char ch){
        int[] result = new int [length]; // Method returns int[] result
        for(int i=0; i<length; i++){
            result[i] = myarray[i].length -
                            myarray[i].replaceAll(ch+"", "").length;
        }
        return result;
    }
    

    代码像这样计算字符串中的字符。担心大写和小写?在您的输入中使用 String.toLowerCase() 和 .toUpperCase()。

    [字符串长度] - [替换字符后的字符串长度]

    查看 cmets 有时会有所帮助。看看 Dave's,你会发现错误。

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2015-06-04
      相关资源
      最近更新 更多