查找小写单词的最高和最低数量

时间:2017-09-10 17:27:35

标签: java

我觉得我很接近,但我现在卡住了。 基本上我需要做的是要求3个字符串,并从输入中找到较小的小写数和小写的最小数

import java.util.*;

public class Practice_program1 {

    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        int x = 0;
        int y = 0;
        int z = 0;
        String user;
        String a = input.nextLine();
        String b = input.nextLine();
        String c = input.nextLine();

        for (int i = 0; i < 3; i++) {
            System.out.print("Enter a String: ");
            user = input.nextLine();

            if (Character.isLowerCase(a.charAt(i)))
                x++;
            else if (Character.isLowerCase(b.charAt(i)))
                y++;
            else if (Character.isLowerCase(c.charAt(i)))
                z++;

            {
                if (x > y && x > z)
                    ;
                System.out.print(user + " has the highest number of lowercase letters" + a);
                if (y > z && y < x)
                    ;
                System.out.print(user + " has the lowerst number of lowercase letters" + b);

            }

        }

    }
}

3 个答案:

答案 0 :(得分:1)

您当前的代码执行如下:

if (x > y && x > z); // no statement executed even if the condition is satified
System.out.print(user + " has the highest number of lowercase letters" + a); // would print to console no matter x,y,z is max of min
if (y > z && y < x); // same as  above
System.out.print(user + " has the lowerst number of lowercase letters" + b); // same as above

另外,要找出最大值,您可以使用Math.max(int,int)作为:

Math.max(x, Math.max(y,z)); // this would return the max out of three

同样,您也可以尝试找出最小值,然后打印出来。

System.out.print(user + " has the lowest number of lowercase letters" + Math.min(x, Math.min(y,z)));

答案 1 :(得分:1)

嗯,在提供解决方案之前,我想指出一些事项。

  1. 我不明白使用String变量“user”。因此,我跳过了它。如果您可以解释它的用法,我可以根据需要相应地编辑代码。
  2. 没有考虑具有相同数量的小写字母的两个字符串的条件。
  3. 另一件事是你试图使用迭代变量i计算小写字母的数量,但如果字符串有三种不同的长度怎么办?
  4. 这就是为什么我认为最好编写一个单独的方法来计算每个字符串中的小写字母数,然后我将它们的计数进行比较,以找到最大小写字母和最小小写字母的字符串。

    public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
        String user;
        String a = input.nextLine();
        String b = input.nextLine();
        String c = input.nextLine();
        input.close();
    
        int countOfA = findLowerCaseNumber(a);
        int countOfB = findLowerCaseNumber(b);
        int countOfC = findLowerCaseNumber(c);
    
        if(countOfA > countOfB && countOfA > countOfC){
            System.out.println(a + " has the highest number of lowercase letters " + countOfA );
            if(countOfB < countOfC)
                System.out.println(b + " has the lowest number of lowercase letters " + countOfB );
            else
                System.out.println(c + " has the lowest number of lowercase letters " + countOfC );
        }
        else if(countOfB > countOfA && countOfB > countOfC){
            System.out.println(b + " has the highest number of lowercase letters " + countOfB );
            if(countOfA < countOfC)
                System.out.println(a + " has the lowest number of lowercase letters " + countOfA );
            else
                System.out.println(c + " has the lowest number of lowercase letters " + countOfC );
        }
        else {
            System.out.println(c + " has the highest number of lowercase letters " + countOfC );
            if(countOfB < countOfC)
                System.out.println(b + " has the lowest number of lowercase letters " + countOfB );
            else
                System.out.println(a + " has the lowest number of lowercase letters " + countOfA );
        }
    }
    
    public static int findLowerCaseNumber(String str){
        int lowerCaseLetters = 0;
        for(int i = 0; i < str.length(); i++)
            // I prefer to use the ASCII values of lower case letters but what you have used is also correct.
            if(str.charAt(i) > 96 && str.charAt(i) < 123)
                lowerCaseLetters++;
        return lowerCaseLetters;
    }
    

    您也可以使用nullpointer提到的Math.max()和Math.min()来派生解决方案,而不是手动比较每个字符串的计数。

答案 2 :(得分:0)

你的任务给我带来了一些问题:
1.循环for (int i = 0; i < 3; i++)表示您只计算输入字符串的前3个字符 - abc。并且每次迭代都会打印结果。是这样的条件问题?
你读了user三次。这是正常的吗?

无论如何,有一种通过流计算小写字符的方式:

private static long getLowerCaseCount(String str) {
    return str.chars().map(i -> (char)i).filter(Character::isLowerCase).count();
}

使用:

long x = getLowerCaseCount(a);
long y = getLowerCaseCount(b);
long z = getLowerCaseCount(c);
long min = Math.min(x, Math.min(y, z));
long max = Math.max(x, Math.max(y, z));