2015-01-08 4 views
-1

안녕하세요 저는 텍스트 문서의 단어 + 정의를 가져 와서 그들을 스크램블 한 다음에 그들을 퀴즈하는 프로그램을 만들고 있습니다. 단어는 (단어 : 정의)로 구성됩니다.
나는 프로젝트를위한 코드를 끝냈다. 그러나 내가 컴파일 한 후에 어떤 이유로 콘솔은 공란으로 남는다.프로젝트가 컴파일되지만 컴파일러의 정보를 표시하지 않습니다.

public class VocabularyWord { 
    private String word, definition; 

public VocabularyWord(String w, String d){ 
    word=w; 
    definition=d; 
} 
public String getDefinition(){ 
    return definition; 
} 
public String getWord(){ 
    return word; 
} 
public String toString(){ 
    return word+" "+definition; 
} 
} 

2 급 : - - :

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Scanner; 

public class VocabularyTest { 
private ArrayList<VocabularyWord>words; 
private int c; 

public VocabularyTest() throws FileNotFoundException{ 
    words=new ArrayList<VocabularyWord>(); 
    ArrayList<String> str=new ArrayList<String>(); 
    File inputFile = new File("Vocabulary.txt"); 
    Scanner input = new Scanner(inputFile); 
    while(input.hasNextLine()){ 
     str.add(input.nextLine()); 
     processStrings(str); 
     for(int i = 0; i<100; i++) 
      swapWords(); 
} 
} 
private void processStrings(ArrayList<String>lines){ 
    int pos=0; 
    for(int i=lines.size()-1; i>=0; i--){ 
     pos=lines.get(i).indexOf(":"); 
     String s=lines.get(i).substring(0, pos); 
     String ss=lines.get(i).substring(pos+1, lines.get(i).length()); 
     VocabularyWord p=new VocabularyWord(s,ss); 
     words.add(p); 
     c++; 
    } 
} 
private void swapWords(){ 
    int x=(int) (Math.random()*words.size()); 
    int xx=(int)(Math.random()*words.size()); 
    while(x==xx) 
     xx=(int)(Math.random()*words.size()); 
    Collections.swap(words, xx, x); 
    } 
public void quiz(){ 
    System.out.println("hi"); 
    int n=0; 
    Scanner kb=new Scanner(System.in); 
    for(int i=words.size()-1; i>=0; i--){ 
     System.out.println(words.get(i).getDefinition()); 
      if(kb.nextLine().equals(words.get(i).getWord())){ 
       System.out.println("Nice Job!"); 
       n++; 
      } 
      else 
       System.out.println(words.get(i).getWord()); 
    } 
    System.out.println("You got "+n+" correct!"); 
} 

} 

등급 3 : -



1 등급 :
코드는 내가 아래에 표시됩니다 세 개의 클래스로 구성
import java.io.FileNotFoundException; 
public class VocabTestTester { 
    public static void main(String[] args)throws FileNotFoundException{ 
     VocabularyTest test= new VocabularyTest(); 
     test.quiz(); 

     } 
} 
+0

는 아마 프로그램을 디버깅 할 때입니다. 당신은 진술한다,''내가 컴파일 한 후에 콘솔은 공백으로 남는다 ... "'- 실제로 ** ** 컴파일 후에 프로그램을 실행합니까? –

+0

죄송합니다. 나는 컴파일하고 실행하는 것을 의미합니다. – nebularis

+0

** ** ** 당신은 그것을 어떻게 운영하고 있습니까? –

답변

0

당신의 VocabularyTest 생성자가 실패하고있는 것 같아요, 아마도이 파일을 예외적으로 보길 원할지라도 파일을 적절히 찾지 못했을 수도 있습니다. 이것을 확인하는 것이 좋을 것입니다.

VocabularyTest 생성자의 첫 번째 줄에 "hi"를 인쇄하고 파일 경로 을 인쇄하여이라는 경로와 일치하는지 확인한 다음 읽은 단어를 콘솔에 인쇄하십시오.

예를 들어,

public VocabularyTest() throws FileNotFoundException{ 
    System.out.println("In VocabularTest Constructor"); 
    words=new ArrayList<VocabularyWord>(); 
    ArrayList<String> str=new ArrayList<String>(); 
    File inputFile = new File("Vocabulary.txt"); 

    System.out.println(inputFile.getAbsolutePath()); 

    Scanner input = new Scanner(inputFile); 
    while(input.hasNextLine()){ 
     System.out.println(input.nextLin()); 
    } 
+0

F *** 예, 생성자에서 오류가 발생했습니다. 닫힌 루프 동안 그리고 우리는 잘했다. 고마워, 좋은 밤을 보내라. (북미에 있다면)! – nebularis

관련 문제