gpt4 book ai didi

java - 文件/变量错误 Java

转载 作者:太空宇宙 更新时间:2023-11-04 06:18:27 26 4
gpt4 key购买 nike

此代码查看数百个名称并查找它们的流行度和含义。我有两个问题。我想要完成的错误所在是将名称含义的文本打印到图表的顶部。1. 如何修复此错误:

BabyNames.java:82: error: cannot find symbol
String meanings = findingStatistics(console,meaningsFile);

2.当findStatistics运行两次时,变量statistics仅保存名称含义的信息。如何访问该名称的受欢迎程度以使用我的图表?现在我已经有了相当于统计的整个方法,这就是意义。

这是我的代码:

import java.util.*;
import java.io.*;
import java.awt.*;

public class BabyNames{ //ADD COMMENTS
public static final int STARTINGYEAR = 1890;
public static final int WIDTH = 60;
public static final int HEIGHT = 30;
private static String nameFinal;
public static void main(String[] args) throws FileNotFoundException{
Scanner console = new Scanner(System.in);
DrawingPanel panel = new DrawingPanel(780,560);
Graphics g = panel.getGraphics();
Scanner nameFile = new Scanner(new File("names.txt"));
Scanner meaningsFile = new Scanner(new File("meanings.txt"));
Scanner nameFile2 = new Scanner(new File("names2.txt"));
intro();
fixedGraph(g);
nameFinal = nameToLowerCase(console);
if(STARTINGYEAR == 1890){
findingStatistics(console,nameFile);;
changingGraph(console,g);
}
else{
findingStatistics(console, nameFile2);
changingGraph(console,g);
}
findingStatistics(console,meaningsFile);
}



public static void intro(){
System.out.println("This program allows you to search through the");
System.out.println("data from the Social Security Administration");
System.out.println("to see how popular a particular name has been");
System.out.println("since 1890" );
System.out.println();
System.out.print("Name: ");
}

public static String nameToLowerCase(Scanner console){
String originalName = console.next();
String name = "" ;
int lengthOfName = originalName.length();
String beginingOfName = originalName.substring(0,1).toUpperCase();
String endOfName = originalName.substring(1,lengthOfName).toLowerCase();
name = beginingOfName + endOfName;
return name;
}
public static String findingStatistics(Scanner console, Scanner data){
// String nameFinal = nameToLowerCase(console);
boolean goesThroughOnce = false; //
String statistics = "";
String currWord = "";
String currLine = "";
while (data.hasNext() && goesThroughOnce == false){
currLine = data.nextLine();
Scanner lineBeingRead = new Scanner(currLine); //make other scanners?? for each file
currWord = lineBeingRead.next(); //

if (currWord.equals(nameFinal) || currWord.equals(nameFinal.toUpperCase())){ //
statistics = currLine;
goesThroughOnce = true;
System.out.println(statistics);
}
else{
}
}
return statistics;
}
public static void fixedGraph(Graphics g){ //Draws fixed things such as gray blocks and black lines
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,780,HEIGHT);
g.fillRect(0,560-HEIGHT,780,HEIGHT);
g.setColor(Color.BLACK);
g.drawLine(0,HEIGHT,780,HEIGHT);
g.drawLine(0,560-HEIGHT,780,560-HEIGHT);
}

public static void changingGraph(Scanner console, Graphics g){
String meanings = findingStatistics(console,meaningsFile);
g.drawString("" + meanings,0,16); //draws meaning text
int startingYear = STARTINGYEAR;
int amountOfDecades = 0;
if(startingYear == 1890){
amountOfDecades = 13;
}
else{
amountOfDecades = 8;
}
g.drawString("" + startingYear,0,552); //fencepost
for(int i=0; i<=amountOfDecades;i++){
int year = startingYear + (10 * i);
g.drawString("" + year,(WIDTH*i),552); //draws decade numbers
}
}
}

如果我在错误所在处添加//并且不包含该错误,只是为了更好地理解代码,对于名称“Aaron”,它应该打印出来:

Name: AARON
Aaron f 0 0 0 0 0 0 0 0 0 883 0 0 0
AARON m English, Biblical From the Hebrew name ??????? ('Aharon) which is most likely of unknown Egyptian origin.

简而言之,我试图让它在图表上打印文本,并为含义和流行度创建两个变量(我将它们都放在统计数据中)。

最佳答案

您的changingGraph方法仅使用console来计算meanings,当然,它还需要meaningsFile。从 changingGraph 中删除对 findingStatistics 的调用会更有意义,特别是因为无论如何您都是从 main 调用它的。

因此,更改 changingGraph 的签名,使其采用 meanings 作为参数,并删除方法主体的第一行。然后它看起来像这样。

public static void changingGraph(String meanings, Graphics g){  
g.drawString("" + meanings,0,16); //draws meaning text
// ... and so on.

然后,当您调用它时,将上次调用返回的值传递给 findingStatistics。因此,在您的 main 方法中,您将拥有这个。

  if(STARTINGYEAR == 1890){
findingStatistics(console,nameFile);
}
else{
findingStatistics(console, nameFile2);
}
String meanings = findingStatistics(console, meaningsFile);
changingGraph(meanings, g);

关于java - 文件/变量错误 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27761140/

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