[英]Strings and text Input/Output
我在编写这个程序时遇到了麻烦(我可能想得太多了!)无论如何,我遇到的问题是我可以用一种方法完成所有要求……但我必须使用其他五种方法……可以谁来帮帮我?!
以下是我得到的指示:
编写一个 Java 程序,该程序将计算并报告字符串中的单词和字符数。 该程序还应显示每个字母的出现次数和所代表总数的百分比。 对于这个程序,假设用户将只输入小写和大写字母。
您的 main 方法必须只有变量声明和调用方法
您的程序必须至少有 5 个称为 stringLength、convertToUpperCaseString、wordCount、charCount 和 OccurenceNPercentage 的方法。
字符串在 JAVA 中是不可变的,您将需要一个新的 convertToUpperCaseString 数组。
' '、'A'、'Z'、'a' 和 'z' 的 ASCII 值是 32、65、90、97 和 122。
我所拥有的:导入 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;
}
}
}
这是一个完整的程序,可以满足您的需求。 但是,它将空格计为字符。 此外, stringLength()和charCount()方法做同样的事情 - 计算字符。 百分比报告方法取自关于堆栈溢出的另一个答案。 有了这个答案,您将不会从家庭作业中学到任何东西。 请至少尝试理解算法。 感谢并享受!
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.