首页 > 解决方案 > 如何获得输出和图形的第二部分?

问题描述

这显示了我需要编写的程序

这是我到目前为止的代码,我不知道如何获取输出的第二部分,它读取第二个文件并输出含义以及如何绘制图形。在此先感谢您,任何提示都会有所帮助。

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);

        }*/
    }

标签: java

解决方案


推荐阅读