프로그램의 셸을 바꿀 수 없으므로 최종 목표는 txt 파일의 단어 목록에서 임의의 단어를 선택하는 것입니다. 나는이 많은 시간 동안 여러 차례 스캔을했고, 코드 하나 하나를 거쳐 여러 가지 다른 것들을 시도했지만, 나는 그것을 실행할 때마다, 문제없이 컴파일하지만 출력을 얻지는 못한다. 나도 개인적인 기능에 약간의 출력을 삽입하려했지만 아무 소용이 없었다. 누구든지 내 코드에 무엇이 잘못되었는지 보거나 어떤 일이 일어 났는지 설명 할 수 있습니까?txt 파일에서 임의의 단어를 가져오고 출력이없고 컴파일러 오류가 발생하지 않습니다. java
는import java.util.*;
class PartOfSpeech
{
private String[] words;
private Random random;
private String filename;
public PartOfSpeech(String filename)
{
this.filename = filename;
this.read();
}
//this picks a random number and uses that number for the index of the array for which to return
public String getRandomWord()
{
int index;
index = random.nextInt(this.getCount());
return words[index];
}
//this gets a count of how many lines of txt are in the file
private int getCount()
{
Scanner fr = new Scanner(this.filename);
int count = 0;
while(fr.hasNextLine())
{
count++;
}
return count;
}
//this creates a scanner and inserts each word from the txt file into an array
private void read()
{
Scanner fr = new Scanner(this.filename);
for(int i=0; i<this.getCount(); i++)
{
words[i] = fr.nextLine();
}
}
public static void main(String[] args)
{
PartOfSpeech n = new PartOfSpeech("nouns.txt");
System.out.print(n.getRandomWord());
}
}
'read' 함수를 호출 한 적이 있습니까? –
constructor의 this.read() – amudhan3093
아, 그래,'getCount'가 무엇을 돌려 주는지? –