整数不能取消引用

我的 Java 简单代码有一些问题。我想做飞机航班搜索程序,但是当我将变量保留在参数下时,出现错误:


Airlines.java:14: error: int cannot be dereferenced

    String Parameters = flightNumber_go.getParameters();

任何人都知道我如何解决这个问题?


附言。对不起,我的英语不好


import java.util.Scanner;


class Airlines{

    public static void main(String args[]) throws Exception{

        Flight 524 = new Flight("Moskwa", "Londyn", 140);

        Flight 135 = new Flight("Warszawa", "Wroclaw", 60);

        Flight 141 = new Flight("Frankfurt", "Rzym", 95);


        Scanner flightNumber = new Scanner(System.in);

        System.out.println("Enter code of your flight: ");

        int flightNumber_go = Integer.valueOf(flightNumber.nextLine());


        String Parameters = flightNumber_go.getParameters();

        System.out.println(Parameters);


    }

}


class Flight{

    String departures;

    String arrival;

    int price;


    public Flight(String departures, String arrival, int price){

        this.departures = departures;

        this.arrival = arrival;

        this.price = price;

    }

    public String getParameters(String ... args){

        return "Lot z "+this.departures+" do "+this.arrival+" kosztuje "+this.price;

    }

}


慕后森
浏览 127回答 1
1回答

皈依舞

首先,您误解了如何正确使用 asObject仅getParameters适用于当前对象,它不会搜索您创建的所有对象。如果要查找特定对象,则必须使用 aCollection来跟踪所有对象。此外,您不应使用变量名来存储信息,而应将该信息作为整数存储在对象内部。这是修改后的Flight类:class Flight {&nbsp; &nbsp; private String departures;&nbsp; &nbsp; private String arrival;&nbsp; &nbsp; private int price;&nbsp; &nbsp; private int flightNum;&nbsp; &nbsp; public Flight(String departures, String arrival, int price, int flightNum){&nbsp; &nbsp; &nbsp; &nbsp; this.departures = departures;&nbsp; &nbsp; &nbsp; &nbsp; this.arrival = arrival;&nbsp; &nbsp; &nbsp; &nbsp; this.price = price;&nbsp; &nbsp; &nbsp; &nbsp; this.flightNum = flightNum;&nbsp; &nbsp; }&nbsp; &nbsp; public String getParameters(){&nbsp; &nbsp; &nbsp; &nbsp; return "Lot z "+this.departures+" do "+this.arrival+" kosztuje "+this.price;&nbsp; &nbsp;}&nbsp; &nbsp; public String getDepartures() {&nbsp; &nbsp; &nbsp; &nbsp; return departures;&nbsp; &nbsp; }&nbsp; &nbsp; public String getArrival() {&nbsp; &nbsp; &nbsp; &nbsp; return arrival;&nbsp; &nbsp; }&nbsp; &nbsp; public int getPrice() {&nbsp; &nbsp; &nbsp; &nbsp; return price;&nbsp; &nbsp; }&nbsp; &nbsp; public int getFlightNum() {&nbsp; &nbsp; &nbsp; &nbsp; return flightNum;&nbsp; &nbsp; }&nbsp; &nbsp; public void setDepartures(String departures) {&nbsp; &nbsp; &nbsp; &nbsp; this.departures = departures;&nbsp; &nbsp; }&nbsp; &nbsp; public void setArrival(String arrival) {&nbsp; &nbsp; &nbsp; &nbsp; this.arrival = arrival;&nbsp; &nbsp; }&nbsp; &nbsp; public void setPrice(int price) {&nbsp; &nbsp; &nbsp; &nbsp; this.price = price;&nbsp; &nbsp; }&nbsp; &nbsp; public void setFlightNum(int flightNum) {&nbsp; &nbsp; &nbsp; &nbsp; this.flightNum = flightNum;&nbsp; &nbsp; }}请注意,我添加了新参数,并使用标准的 getter 和 setter 来访问flightNum所有类变量。private现在这里是修改后的Airlines类,它使用 anArrayList来存储Flights:class Airlines {&nbsp; &nbsp; public static void main(String args[]) throws Exception{&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Flight> flights = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; flights.add(new Flight("Moskwa", "Londyn", 140, 524));&nbsp; &nbsp; &nbsp; &nbsp; flights.add(new Flight("Warszawa", "Wroclaw", 60, 135));&nbsp; &nbsp; &nbsp; &nbsp; flights.add(new Flight("Frankfurt", "Rzym", 95, 141));&nbsp; &nbsp; &nbsp; &nbsp; Scanner flightNumber = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter code of your flight: ");&nbsp; &nbsp; &nbsp; &nbsp; int flightNumber_go = Integer.valueOf(flightNumber.nextLine());&nbsp; &nbsp; &nbsp; &nbsp; Flight currentFlight = findFlight(flights, flightNumber_go);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(currentFlight.getParameters());&nbsp; &nbsp; }&nbsp; &nbsp; public static Flight findFlight(ArrayList<Flight> flights, int flightNum) {&nbsp; &nbsp; &nbsp; &nbsp; for (Flight f : flights) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (f.getFlightNum() == flightNum) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return f;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //If no flights are found&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp;}}我添加了一个static名为的方法findFlight,该方法将ArrayList您flightNumber想要查找的作为参数,并返回相应的Flight. 这是使用一个简单的增强for循环完成的。null如果未找到航班,该方法将返回,可以修改它以返回您想要的默认情况下的任何内容。示例运行:输入您的航班代码:135从华沙到弗罗茨瓦夫的航班费用为 60注意: 在您的情况下,使用 aMap将密钥存储为航班号和 asFlight值可能是有意义的,以确保密钥是唯一的,然后您本身就不需要flightNumber了Object。这ArrayList只是一种方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java