gpt4 book ai didi

Java 错误方法未定义 Echo 类型

转载 作者:太空宇宙 更新时间:2023-11-04 14:57:17 25 4
gpt4 key购买 nike

我在代码“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com