【问题标题】:Project compiles but not displaying info in compiler项目编译但未在编译器中显示信息
【发布时间】:2015-01-08 04:23:39
【问题描述】:

嘿,我正在制作一个程序,它从文本文档中获取单词+定义,对其进行打乱,然后对您进行测验。单词的结构为(单词:定义)。
我完成了项目的代码,但由于某种原因,我编译后控制台保持空白。
代码由三个类组成,我将在下面显示:
第 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 类:-

 import java.io.FileNotFoundException;
 public class VocabTestTester {
    public static void main(String[] args)throws FileNotFoundException{
        VocabularyTest test= new VocabularyTest();
        test.quiz();

        }
}

【问题讨论】:

  • 也许是时候调试你的程序了。你说,"the console stays blank after I compile..."——你真的在编译后运行程序吗?
  • 对不起,我的意思是编译并运行。
  • 如何你运行它?
  • 在 Eclipse 中按下运行图标。

标签: java oop arraylist


【解决方案1】:

我猜你的 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());
    }

【讨论】:

  • F*** 是的,在构造函数中发现错误。关闭while循环,然后我们就很好了。谢谢伙计,晚安(如果在北美)!
猜你喜欢
  • 2018-12-29
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多