여러 줄의 텍스트를 가져와 각 줄마다 각 문자의 개수를 계산해야합니다. 마지막 줄은 한 마디로 끝나야합니다. 나는 길이가 26 인 int 배열을 만들도록 요청 받았는데, arrayName[0] = number of a's
, arrayName[1] = number of b's
등과 같은 대소 문자는 무시해야한다. 각 문자의 발생을 확인하고 정확한 발생 횟수로 색인 된 변수를 정의하는 데 문제가 있습니다. 지금까지 내 코드 :java - 문자열의 문자 수를 int 배열로 정렬 26
import java.util.Scanner;
public class LetterCounter
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String[] textBlock = new String[10];
System.out.println("Enter text.");
int index;
//Allows me to type in something for all "lines" of text until I end one with a period.
for(index = 0; index < textBlock.length; index++)
{
textBlock[index] = input.nextLine();
if(textBlock[index].contains("."))
{
System.out.println("Period found");
break;
}
}
//Outputs what the user printed except for the lines not typed in (they return null).
System.out.println("Text input by user:");
for(index = 0; index < textBlock.length; index++)
{
if(textBlock[index] != null)
{
System.out.println(textBlock[index]);
}
}
//int array
int[] occurrences = new int[26];
//used to turn letter into number
char letter = 'a' - 49;
char letter2 = 'b' - 49;
char letter3 = 'c' - 49;
//checks that numbers are correct (return "0" for a, "1" for b, and "2" for c)
System.out.println("Number value of a: " + letter);
System.out.println("Number value of b: " + letter2);
System.out.println("Number value of c: " + letter3);
}// End of main
}//End of program
, 나는 당신의 선언을 할 생각 루프 외부의 배열. 당신은 아마 당신이 소문자를 읽는 각 편지를 강요하고 싶다. 그런 다음 "a"에서 'z'로 문자를 검사 한 후, 읽기 문자의 색인 인 ++ 배열을 'a'로합니다. –
비슷한 질문이 있습니다. http://stackoverflow.com/questions/33770180/how-do -i-store-these-printed-letters-an-array/33770240 # 33770240 – markspace