简体   繁体   English

虽然程序中的循环无法按预期工作并卡住

[英]While Loop in program not working as expected and gets stuck

I am a beginner and would appreciate some advice.我是初学者,希望得到一些建议。 I am writing a program where an airport worker and input plane information and then an airport user can print this information out.我正在编写一个程序,机场工作人员输入飞机信息,然后机场用户可以打印出这些信息。 The program should keep asking the user which option should be selected until the user types 'x' to exit the program.该程序应不断询问用户应选择哪个选项,直到用户键入“x”退出程序。

Look at the while loop under the startAirplanePanel() method.查看 startAirplanePanel() 方法下的 while 循环。

Here is my UserInterface class:这是我的用户界面 class:

import java.util.HashMap;
import java.util.Scanner;
import java.util.ArrayList;

public class UserInterface {
    private Flights fly;
    private Scanner reader = new Scanner(System.in);  

    public UserInterface(){
        this.fly = new Flights();
        this.reader = reader;  
    }   

    public void startAirplanePanel(){        

        System.out.println("Airport panel");
        System.out.println("---------------");
        System.out.println();
        System.out.println();  

        while(true){
        System.out.println("Choose operation: ");
        System.out.println("[1] Add airplane");
        System.out.println("[2] Add flight");
        System.out.println("[x] Exit");

        String input = reader.nextLine();
                if(input.equals("x")){
            break;
        }

        if(Integer.parseInt(input) == 1){
            addPlane();            

        } if(Integer.parseInt(input) == 2){
            addFlight();        
        }
    }        

        System.out.println("Flight service ");
            System.out.println("--------------");
            System.out.println();
            System.out.println();

         while(true){

            System.out.println("Choose operation: ");
            System.out.println("[1] Print planes");
            System.out.println("[2] Print flights");
            System.out.println("[3] Print plane info");
            System.out.println("[x] Print Quit");

            if(reader.equals("x")){
                break;
            }
            if(Integer.parseInt(reader.nextLine()) == 1){
                printPlane();

            }
            if(Integer.parseInt(reader.nextLine()) == 2){
                printFlight();
            }
            if(Integer.parseInt(reader.nextLine()) == 3){
                printPlaneInfo();
            }


        }

}

    public void addPlane(){
        System.out.println("Give plane ID: ");
        String id = reader.nextLine();
        System.out.println("Give plane capacity: ");
        int capacity = Integer.parseInt(reader.nextLine());

        fly.planeMap(id,capacity);      
    }

    public void addFlight(){
        System.out.println("Give plane ID: ");
        String id = reader.nextLine();
        System.out.println("Give departure airport code: ");
        String departure = reader.nextLine();
        System.out.println("Give destination airport code: ");
        String destination = reader.nextLine();     

        fly.flightMap(id,departure,destination);
    }

    public void printPlane(){

        for(int i = 0; i < fly.planeList().size(); i++){
            System.out.println(fly.planeList().get(i));
        }
    }

    public void printFlight(){
        for(int i = 0; i < fly.flightList().size(); i++){
            System.out.println(fly.flightList().get(i));
        }       
    }

    public void printPlaneInfo(){
        System.out.print("Give plane ID: ");
        String id = reader.nextLine();

        System.out.println(id + " (" + fly.planeInfo(id) + ")");
    }

}

Here is the code for the Flights class, which contains the methods:以下是航班 class 的代码,其中包含以下方法:


import java.util.HashMap;

import java.util.ArrayList;

public class Flights {

    public HashMap<String,Integer> plane = new HashMap<String,Integer>();
    public HashMap<String,String> flight = new HashMap<String,String>();

    public Flights(){

        this.plane = plane;
        this.flight = flight;

    }
    public void planeMap(String id, Integer capacity){
        plane.put(id, capacity);
    }

    public void flightMap(String id, String departure, String destination){
        String flight1 = departure + "-" + destination;
        flight.put(id, flight1);
    }

    public ArrayList planeList(){
        ArrayList<String> keylist = new ArrayList<String>(plane.keySet());
        ArrayList<Integer> valuelist =  new ArrayList<Integer>(plane.values());
        ArrayList<String> newlist = new ArrayList<String>();

        for(int i = 0 ; i < keylist.size() ; i++){
            newlist.add(keylist.get(i) + " (" + valuelist.get(i) + "ppl)");
        }

        return newlist;
    }

    public ArrayList flightList(){
        ArrayList<String> keylist = new ArrayList<String>(flight.keySet());
        ArrayList<String> valuelist =  new ArrayList<String>(flight.values());
        ArrayList<String> newlist = new ArrayList<String>();

        for(int i = 0; i < keylist.size(); i++){
            newlist.add(keylist.get(i) + " (" + plane.containsKey(keylist.get(i)) + "ppl) " + "(" + valuelist.get(i) + ")");
        }        

        return newlist;
    }

    public int planeInfo(String id){

        if(plane.containsKey(id)){
        return plane.get(id);
    }
        return 0;
}

}

And here is the output:这是 output:

Airport panel
---------------


Choose operation: 
[1] Add airplane
[2] Add flight
[x] Exit
1
Give plane ID: 
GA-HAS
Give plane capacity: 
33
Choose operation: 
[1] Add airplane
[2] Add flight
[x] Exit
x
Flight service 
--------------


Choose operation: 
[1] Print planes
[2] Print flights
[3] Print plane info
[x] Print Quit
1
GA-HAS (33ppl)

The 2nd While loop should keep asking the user to choose an operation, for example, after typing in 1 and the system prints "GA-HAS (33ppl)", it should ask the user again for "Choose operation: [1] print planes... [3] Print Quit]. But the while loop just stops there and I can't find the reason why.第二个 While 循环应该不断要求用户选择一个操作,例如,在输入 1 并且系统打印“GA-HAS (33ppl)”后,它应该再次询问用户“选择操作:[1] 打印平面... [3] Print Quit]. 但是 while 循环就停在那里,我找不到原因。

Please advise.请指教。 Thank you!谢谢!

Your while loop hasn't stopped, it's waiting for input.您的 while 循环没有停止,它正在等待输入。

You call nextLine in each of your if conditions which means the program is waiting for more input.您在每个 if 条件中调用nextLine ,这意味着程序正在等待更多输入。 It won't continue until it gets it.它不会继续,直到它得到它。

You got it right with your first while loop.您在第一个 while 循环中做对了。 Accept one input at the top of the loop, and then compare it as necessary.在循环的顶部接受一个输入,然后根据需要进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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