简体   繁体   中英

How can I fix this program?

I have been working on this program with these instructions:

Station Information Write a program that gives information about underground stations. The user should first input how many stations they wish to ask about and then be allowed to name that many stations. The program should say whether they have step free access and the distance that must be travelled to get from entrance to platform. A new type called Station must be created (a record type) and each separate piece of information about a station should be stored in a separate field of the record (its name - a String, whether they have step-free access - a boolean, and the distance in metres - an integer).

A separate method must be written that given a String (a station name) as argument returns a String containing the correct information about the station. The String should be printed by the calling method. An example run of the program: Your answer need only include the information about known stations as in this example.

How many stations do you need to know about? 4

What station do you need to know about? Stepney Green
Stepney Green does not have step free access.
It is 100m from entrance to platform.

What station do you need to know about? Kings Cross
Kings Cross does have step free access.
It is 700m from entrance to platform.

What station do you need to know about? Oxford Street
Oxford Street is not a London Underground Station.

What station do you need to know about? Oxford Circus
Oxford Circus does have step free access.
It is 200m from entrance to platform.

The program doesn't have any errors but doesn't show me the results I need.

This is what I get using the same example:

how many stations do you want to know about?
4
what station do you want to know about?
Stepney Green
Stepney Green does not have step free access. it is 100m away from the entrance to platform.
what station do you want to know about?
Kings Cross
Kings Cross does have step free access. it is 700m away from the entrance to platform.
what station do you want to know about?
Oxford Street
Oxford Street is not a London underground station
what station do you want to know about?
Oxford Circus
Oxford Circus does have step free access. it is 200m away from the entrance to platform.

"Stepney Green" and "stepney green" are not the same string. Case matters. You need to use the equalsIgnoreCase method from the String class, as in, for example

if (station.equalsIgnoreCase("stepney green")) {
    System.out.println(stepneyGreenMessage);
}

About the missing newline in the output message you can just add a literal \\n where you need it to go to a new line. But I would rewrite create_message (which should really be called createMessage to respect java naming conventions) like this to avoid repetitions:

public static String create_message(Station station) {
    String message = station.name + " does ";
    if (!station.step_free_access) { // try to avoid " == true" in if conditions
        message += "not ";
    }
    message += "have step free access.\n"; // notice the "\n" to go to a newline
    message +=  "It is " + station.distance_from_platform + "m away from the entrance to platform.";
    return message;
}

There are other optimizations that could be added (like using a StringBuilder instead of a String but I think they go beyond the exercise you're trying to solve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM