Java 错误方法未定义 Echo 类型

标签 java error-handling

我在代码“e.results();”的第 11 行收到错误 这是我的驱动程序类

import java.util.Scanner;
import java.io.*;
public class LineScrabbleDriver {
  public static void main(String args[]) throws IOException {
   String fileName;
   Scanner nameReader = new Scanner(System.in);
   System.out.println("Enter a file name");
   fileName = nameReader.nextLine();
   Echo e = new Echo(fileName);
   e.readLines();
   e.results();
  }
}

这是Echo的扩展类

import java.io.*;
public class LineScrabble extends Echo{
  double max = 0.0;
  String bestWord = "";
  int lineNumer = 0;
  public LineScrabble(String f) throws IOException {
    super(f);
  }
//scrabbles
int[] scrabbles = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
//process the given line
public void processLine(String s) {
  s.toLowerCase();
  int score = 0;
  for(int i = 0; i<s.length(); i++){
    char ch = s.charAt(i);
    if(Character.isLetter(ch)){
      int pos = ch - 'a';
      score += scrabbles[pos];
      }
    }
  if(score > max){
    max = score;
    bestWord = s;
  }
}
//displays the winner and score
public void results() {
System.out.println("Winner: " + bestWord);
System.out.println("score: " + max/bestWord.length());
}
}

这是 Echo 类

import java.util.Scanner;
import java.io.*;

  public class Echo{
    String fileName; // external file name
    Scanner scan; // Scanner object for reading from external file

    public Echo(String f) throws IOException
    {
     fileName = f;
     scan = new Scanner(new FileReader(fileName));
   }

   public void readLines(){ // reads lines, hands each to processLine
     while(scan.hasNext()){
       processLine(scan.nextLine());
     }
     scan.close();
  }

  public void processLine(String line){ // does the real processing work
     System.out.println(line);
   }
 }

我不太清楚为什么它会给我一个未定义的类型错误。我是java编程新手。请帮忙!

最佳答案

因为您使用的是原始的 Echo 而不是 LineScrabble,我认为您想要类似 -

// Echo e = new Echo(fileName); //
LineScrabble e = new LineScrabble(fileName);
e.readLines();
e.results(); // <-- Echo does not have results() you could add it there, or 
             // use LineScrabble.

关于Java 错误方法未定义 Echo 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23050591/

相关文章:

java - 如何读取 Google App Engine (JAVA) 中的文件

r - 将数据从R写入Redshift问题

vba - 如何排除#VALUE!或#DIV/0!在使用 VBA 计算平均值的列中

javascript - 使用jQuery修改样式时,能否捕获到HTTP错误?

windows - TYPE_E_BUFFERTOOSMALL 和 DISP_E_BUFFERTOOSMALL HRESULT 值之间有什么区别?

java - 从 Java 调用 PLSQL 过程

java - 将 ElasticSearch 与 Spring Data 结合使用时自动生成数字 id

java - 如何使用java.lang.Enum

Java 流将对象列表收集到存储桶

php - 有没有一种干净的方法将 undefined variable 用作PHP中的可选参数?