java - 如何获得输出和图形的第二部分?
问题描述
这是我到目前为止的代码,我不知道如何获取输出的第二部分,它读取第二个文件并输出含义以及如何绘制图形。在此先感谢您,任何提示都会有所帮助。
import java.util.*;
import java.awt.*;
import java.io.*;
public class BabyNames {
//class constants for open area in graphics (Do NOT change)
private static final Integer OPEN_AREA_WIDTH = 780;
private static final Integer OPEN_AREA_HEIGHT = 500;
//prompt msg class constants (Do NOT change)
private static final String MESSAGE_PREFIX = "This program allows you to search through the\n" +
"data from the Social Security Administration\n" +
"to see how popular a particular name has been\n" +
"since ";
// class constant for meaning file (Do NOT change)
private static final String MEANING_FILENAME = "meanings.txt";
// class constant for name file (Change the value only. Do NOT change the names of the constant)
// Test with both "names.txt" and "names2.txt" (Before submission, change back to "names.txt")
private static final String NAME_FILENAME = "names.txt";
// Other class constants (Change the value only. Do NOT change the names of the constants)
private static final Integer STARTING_YEAR = 1890; // change the value according to spec
private static final Integer DECADE_WIDTH = 60; // change the value according to spec
private static final Integer LEGEND_HEIGHT = 30; // change the value according to spec
// YOU ARE NOT ALLOWED TO ADD ANY OTHER CONSTANTS THAN ABOVE
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
Scanner input1 = new Scanner(new File("names.txt"));
Scanner input2 = new Scanner(new File("meanings.txt"));
System.out.println(MESSAGE_PREFIX + STARTING_YEAR + ".");
String name = takeName(console);
String gender = takeGender(console);
String look = lookName(input1, name, gender);
String mean = lookMean(input2, name, gender);
graph();
}
public static String takeName(Scanner console) {
System.out.print("Name:");
String name = console.next();
return name;
}
public static String takeGender(Scanner console) {
System.out.print("Gender (M or F):");
String gender = console.next();
return gender;
}
public static String lookName(Scanner input1, String name, String gender) {
while (input1.hasNext()) {
String text = input1.nextLine();
Scanner search = new Scanner(text);
gender = gender.toUpperCase();
if (search.next().equalsIgnoreCase(name) && (search.next().equals(gender))) {
System.out.println(text);
}
}
return (" not found");
}
public static String lookMean(Scanner input2, String name, String gender) {
while ( input2.hasNext()) {
String text2 = input2.nextLine();
Scanner search = new Scanner(text2);
gender = gender.toUpperCase();
if (search.next().equalsIgnoreCase(name) && (search.next().equals(gender))) {
System.out.println(text2);
}
}
return (" not found");
}
/*public static void graph() {
DrawingPanel panel = new DrawingPanel(780, 560);
panel.setBackground(Color.white);
Graphics g = panel.getGraphics();
g.drawLine(DECADE_WIDTH, DECADE_WIDTH - 10, DECADE_WIDTH, LEGEND_HEIGHT);
g.drawLine(DECADE_WIDTH, LEGEND_HEIGHT, LEGEND_HEIGHT + 100, LEGEND_HEIGHT);
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, 720 + OPEN_AREA_WIDTH, OPEN_AREA_HEIGHT);
g.fillRect(0, 560 - OPEN_AREA_HEIGHT, 720 + OPEN_AREA_WIDTH, OPEN_AREA_HEIGHT);
g.setColor(Color.BLACK);
g.drawLine(0, OPEN_AREA_HEIGHT, 720 + OPEN_AREA_WIDTH, OPEN_AREA_HEIGHT);
g.drawLine(0, 560 - OPEN_AREA_HEIGHT, 720 + OPEN_AREA_WIDTH, 560 - OPEN_AREA_HEIGHT);
}*/
}
解决方案
推荐阅读
- php - problems with sql statements
- c++ - 粒子和 OpenGL 的问题,没有绘制任何内容
- php - URL 重写删除 url 结尾
- dart - 使用嵌套在结果中的不同类型解析 json {"result": DIFFERENTS TYPES}}
- html - 使用 aria-hidden 或 role="presentation" 会提高使用辅助技术的人的导航速度吗?
- c++ - 为什么下面的程序不选择与第一个模板参数相同类型的参数?
- visual-studio - 连接到数据库的问题 - Sql Server 和 Visual Studio C#
- javascript - 如何在使用 Google Sheets API 4 (JavaScript) 导入期间重新排列列或指定列输出
- oop - 继承的对象在 Lua 中共享相同的表属性
- batch-file - cmd.exe 和批处理文件中的目录 /b /s 不同