字符串和文本输入/输出

时间:2015-07-31 17:28:13

标签: java

我在编写这个程序时遇到了麻烦(我可能会过度思考它!)无论如何,我遇到的问题是我可以用一种方法做所有问题......但我必须使用其他五种方法。 ..有人可以帮助我吗?!

以下是我给出的指示:

编写一个Java程序,它将计算并报告字符串中的单词和字符数。该程序还应显示每个字母的出现次数和所代表的总数的百分比。对于此程序,假设用户只输入小写和大写字母。

  1. 您的main方法必须只有变量声明和调用方法

  2. 您的程序必须至少有5个名为stringLength的方法, convertToUpperCaseString,wordCount,charCount和 OccurenceNPercentage。

  3. 字符串在JAVA中是不可变的,你需要一个新的convertToUpperCaseString数组。

  4. '','A','Z','a'和'z'的ASCII值分别为32,65,90,97和122.

  5. 我拥有的: import java.util.Scanner; 公共类Project_Strings_TextIO {

    //initiate main method and call the rest of the methods used
        public static void main(String[] args){
    
            //call all methods
    
    
        }
    
    //determine the length of the string
    public static void stringLength(int length){
    
        //initiate scanner and prompt user for a string
        Scanner input = new Scanner(System.in);
    
        System.out.println("Please enter a string! --> ");
        String phrase = input.nextLine();
    
        //determine length of said string
        System.out.print("String length is " + phrase.length() + " characters long.");
    }
    
    //convert the string to uppercase
    public static void convertToUpperCase(int uppercase){
    
        //upper case
        System.out.print("Look! I can uppercase your string: " + phrase.toUpperCase());
    
    }
    
    //count the words in the string
    public int wordCount(String word){
        if(word == null){
            return 0;
        }
        String input = word.trim();
        int count = input.isEmpty() ? 0 : input.split("\\s+").length;
        return count;
    
        //use this? System.out.print("There are " + phrase.charAt(length) + " words in the string.");
    
    }
    
    //count the characters in the string
    public static void charCount(int phrase){
        System.out.println(phrase.charAt(i));       
    }
    
    //count the occurrences and percentage of the characters in the string
    public static int occurrenceNPercentage(int percent){
        int count = 0;
    
        //use percent for the loop???
        for(int i = 0; i < percent.length(); i++){
            if(percent.charAt(i) == char){
                count++;
            }
            return count;
        }
    }
    

    }

1 个答案:

答案 0 :(得分:0)

This is a complete program that does what you need. However, it counts spaces as characters. Also, stringLength() and charCount() methods do the same thing - counting the characters. The percentage report method is taken from another answer on stack overflow. With this answer, you will learn nothing from your homework. Please at least try to understand the algorithms. Thanks and enjoy!

public class wordReport {

    //determine the length of the string
    public static void stringLength(String inputString) {

        //determine length of said string
        System.out.println("String length is " + inputString.length() + " characters long.");
    }

    //convert the string to uppercase
    public static void convertToUpperCase(String inputString) {

        //upper case
        System.out.println("Look! I can uppercase your string: " + inputString.toUpperCase());

    }

    //count the words in the string
    public static void wordCount(String inputString) {
        String[] wordsArray = inputString.split(" ");
        System.out.println("There are " + wordsArray.length + " words in the string.");

    }

    //count the characters in the string
    public static void charCount(String inputString) {

        System.out.println("There are " + inputString.length() + " characters in the string.");

    }

    //count the occurrences and percentage of the characters in the string
    public static void occurrenceNPercentage(String inputString) {
        char[] capital = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

        char[] small = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        int[] count = new int[26];
        char[] chars = inputString.toCharArray();
        int myTotal = 0;
        for (int i = 0; i < chars.length; i++) {
            for (int j = 0; j < 26; j++) {
                if (chars[i] == capital[j] || chars[i] == small[j]) {
                    count[j]++;
                    myTotal = myTotal + 1;
                    break;
                }
            }
        }

        System.out.println("Comlete count");
        for (int i = 0; i < 26; i++) {
            System.out.print(" " + small[i]);
            System.out.print(" " + count[i]);
            if (count[i] > 0) {
                System.out.println(" " + (((float) count[i] / myTotal) * 100) + "%");
            } else {
                System.out.println(" 0%");
            }
            //calculate percentage for the full count
        }
    }

    public static void main(String[] args) {

        System.out.println("Please enter String");

        Scanner input = new Scanner(System.in);
        String inputString = input.nextLine();
        stringLength(inputString);
        convertToUpperCase(inputString);
        wordCount(inputString);
        charCount(inputString);
        occurrenceNPercentage(inputString);

    }

}