使用方法的Java Couting字符
我试图将第一个for循环放入它自己的私有方法中,而我似乎无法在不破坏输入的情况下获得它。目标只是让main方法用于调用方法,并在方法中完成所有实际计算 如有任何指导,将不胜感激 以下是我的部分代码:使用方法的Java Couting字符,java,Java,我试图将第一个for循环放入它自己的私有方法中,而我似乎无法在不破坏输入的情况下获得它。目标只是让main方法用于调用方法,并在方法中完成所有实际计算 如有任何指导,将不胜感激 以下是我的部分代码: import java.util.Arrays; import java.util.Scanner; public class CountChars { static int[] alphabetArray = new int[26]; public static void main(String
import java.util.Arrays;
import java.util.Scanner;
public class CountChars {
static int[] alphabetArray = new int[26];
public static void main(String[] args) {
int linenum = 0;
Scanner input = new Scanner(System.in);
while(input.hasNext()){
String userInput =input.nextLine();
String input1 = userInput.toLowerCase();
for ( int i = 0; i < input1.length(); i++ ) {
char ch= input1.charAt(i);
int value = (int) ch;
if (value >= 97 && value <= 122){
alphabetArray[ch-'a']++;
}
}
int others= counts(++linenum, userInput);
printLetterCounts();
String yesorno= anyVowels(userInput);
String VowCont= countVowels (userInput);
int Contcount= countConsonants(0, userInput);
zeroLetterCount();
System.out.print(" others="+others); // ADDED THIS LINE
System.out.println();
导入java.util.array;
导入java.util.Scanner;
公共类CountChars{
静态int[]alphabetArray=新int[26];
公共静态void main(字符串[]args){
int linenum=0;
扫描仪输入=新扫描仪(System.in);
while(input.hasNext()){
字符串userInput=input.nextLine();
String input1=userInput.toLowerCase();
对于(int i=0;i 如果(value>=97&&value如果在方法中需要行计数器,则可以执行以下操作:
linenum = printLineCount(linenum);
[...]
protected int printLineCount(int lineCount) {
System.out.printf("Line %02d:", lineCount);
return lineCount + 1;
}
同时,您唯一的换行符System.out.println()
在while循环的外部。应该在内部,因此每行输入创建一行
仔细想想:您想要的输出要求您首先处理所有输入,然后才创建输出。因此,也许您应该首先将输入行填充到列表中,并对其进行操作以生成输出?这一实现也不正确/没有任何意义:
private static void zeroLetterCount(){
for (int i=0;i<alphabetArray.length;i++)
alphabetArray[i] = 0;
}
将您的main更改为:
public static void main(String[] args) {
int linenum = 0;
Scanner input = new Scanner(System.in);
while(input.hasNext()){
String userInput =input.nextLine();
String input1 = userInput.toLowerCase();
for ( int i = 0; i < input1.length(); i++ ) {
char ch= input1.charAt(i);
int value = (int) ch;
if (value >= 97 && value <= 122){
alphabetArray[ch-'a']++;
}
}
int others= counts(++linenum, userInput); // THIS LINE CHANGE AND MOVED UP.
countAllChars();
String yesorno= anyVowels(userInput);
String VowCont= countVowels (userInput);
int Contcount= countConsonants(0, userInput);
zeroLetterCount();
System.out.print(" others="+others); // ADDED THIS LINE
System.out.println();
}
}
输出:
Line 01:d=1 e=4 f=3 h=2 v=1 w=2 YES vowels=4 Consonants=9 others=0
Line 02:h=2 r=3 w=2 NO vowels=0 Consonants=7 others=10
行号仍然没有随着每个用户的输入而增加。我每次都需要增加1。如果我需要将字符计数部分和字符分解的打印放在自己的方法中怎么办?我正在尝试将第一个for循环放在自己的方法中,如果不弄乱我的输入,就无法得到它。有什么指导吗?
private static void countAllChars() {
for (int i = 0; i < alphabetArray.length; i++) {
if(alphabetArray[i]>0) {
char ch = (char) (i+97);
System.out.print(ch +"="+alphabetArray[i]+ " ");
}
}
}
hevdhfewfewfe
hhwwrrr73##$6%&%7
Line 01:d=1 e=4 f=3 h=2 v=1 w=2 YES vowels=4 Consonants=9 others=0
Line 02:h=2 r=3 w=2 NO vowels=0 Consonants=7 others=10