【问题标题】:Count and Display UpperCase, LowerCase and Numbers计数并显示大写、小写和数字
【发布时间】:2016-02-22 07:29:56
【问题描述】:

我的任务是在我的 java 类中做这个。而且我不知道该怎么办。

使用 Scanner 或 JOptionPane,它会要求用户输入一个单词。然后它将验证单词是否由大写字母、数字、空格组成,然后计算字符总数。

输入一个单词:_____

输入的单词:“在此处显示单词”

找到的字符:

大写:

大写字母总数:

小写:

小写字母总数:

数字:

数字总数:

空格数:

找到的字符总数:

【问题讨论】:

标签: java


【解决方案1】:

可能应该是基础课程中涵盖的内容...已编辑为包含数字计数器和ScannerJOptionPane 的相关代码。

public static void main(String[] args) {

    String input = (String) JOptionPane.showInputDialog(null, "Input a sentence.", "Dialogue", JOptionPane.PLAIN_MESSAGE, null, null, null);

    // System.out.println("Input a word.");
    // @SuppressWarnings("resource") Scanner scan = new Scanner(System.in);
    // String input = scan.nextLine();

    System.out.println("Input word was: " + input);

    int length = input.length();
    char[] charAnalysis = input.toCharArray();

    int whitespace = 0;
    int lowercase = 0;
    int uppercase = 0;
    int numberCount = 0;
    for (char element : charAnalysis) {
        if (Character.isWhitespace(element)) {
            whitespace++;
        } else if (Character.isUpperCase(element)) {
            uppercase++;
        } else if (Character.isLowerCase(element)) {
            lowercase++;
        } else if (Character.isDigit(element)) {
            numberCount++;
        }
    }

    System.out.println("Length: " + length);
    System.out.println("Uppercase letters: " + uppercase);
    System.out.println("Lowercase letters: " + lowercase);
    System.out.println("Digit count: " + numberCount);
    System.out.println("Whitespaces: " + whitespace);
}

【讨论】:

  • 尼特:如果使用扫描仪,请不要关闭scan。您没有自己打开底层流;您不知道后续代码要如何使用该流。
  • 问题已解决。虽然,我不使用流。以前发生过错误,因此可能是最佳实践。 (不过 Eclipse 应该不会再抱怨了!)
【解决方案2】:
             String word = JOptionPane.showInputDialog(frame, "enter word");
for(int i =0;i<word.length();i++)
{
        if (Character.isUpperCase(word.charAt(i))){ upperCase++; }
                        else if (Character.isLowerCase(word.charAt(i))){ lowerCase++; }
                        else if (Character.isDigit(word.charAt(i)))    { numberCount++;}
                        else if(charAt(i)=' ')     {spaceCount++}
}
    //to display word count
    System.out.println(word.length);
    //to display uppercase count
    System.out.println(upperCase);
    //to disply digits count
    System.out.println(numberCount);
    //to display space count
    System.out.println(spaceCount);

【讨论】:

  • 不适用于 word.charAt(i) 并且代码中没有任何内容可以遍历 i 的不同变量。
  • 我忘了循环抱歉
【解决方案3】:

试试这个并阅读课程Character以清楚地理解。

import java.util.*;
import java.lang.*;
import java.io.*;

public class HelloWorld{

    public static void main(String[] args) {
    System.out.println("Input a word.");
    Scanner scan = new Scanner(System.in);
    String input = scan.nextLine();
    System.out.println("Entered Word: " + input);
    int length = input.length();
    char[] chars = input.toCharArray();
    int whitespaceLength = 0;
    String upercase = "";
    String lowercase = "";
    String numbers = "";
    for (char element : chars) {
        if (Character.isWhitespace(element)) {
            whitespaceLength++;
        } else if (Character.isUpperCase(element)) {
            upercase+=element;
        } else if (Character.isLowerCase(element)) {
            lowercase+=element;
        }else if (Character.isDigit(element)) {
            numbers+=element;
        }
    }
    System.out.println("Uppercase: " + upercase );
    System.out.println("total number of uppercase letters: " + upercase.length());
    System.out.println("Lowercase: " + lowercase);
    System.out.println("total number of lowercase letters: " + lowercase.length());
    System.out.println("Numbers: " + numbers);
    System.out.println("total number of Numbers: " + numbers.length());
    System.out.println("Number of Whitespaces: " + whitespaceLength);
    System.out.println("Total number of characters found: " + input.length());



    }
}

【讨论】:

    【解决方案4】:
    public class Count {
    public static void main(String[] args) {
        int upperCase = 0;
        int lowerCase = 0;
        int spaceCount = 0;
        int numberCount = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("enter a string ");
        String word = sc.nextLine();
        sc.close();
        for (int i = 0; i < word.length(); i++) {
            if (Character.isUpperCase(word.charAt(i))) {
                upperCase++;
            } else if (Character.isLowerCase(word.charAt(i))) {
                lowerCase++;
            } else if (Character.isDigit(word.charAt(i))) {
                numberCount++;
            } else if (word.charAt(i) == ' ') {
                spaceCount++;
            }
        }
    
        System.out.println("total number of uppercase letters :"+upperCase);
        System.out.println("total number of lowerCase letters :"+lowerCase);
        System.out.println("total number of Numbers :"+numberCount);
        System.out.println("Total number of whitSpaces  :"+spaceCount);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 2021-12-08
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多