繁体   English   中英

在 Java 中执行 toUpperCase 方法后程序随机终止

[英]Program randomly terminates after executing toUpperCase method in Java

我的 Java 程序在使用System.out.println();后终止System.out.println();

我已经将System.out.println放在我的代码中的几个地方,以找出它终止的确切位置,并且它似乎在执行println后立即终止

package exercises;
import java.util.Scanner;

public class TrainSeatBookingApplication {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SeatType theSeatType;
        FloorGrid floorType;
        TrainWay aTrainWay = null;
        TrainSmart aTrainSmart = null;
        Seat customerSeat;
        char planeSizeChoice;
        char seatingArea;
        char seatEconomyOrFirst;
        char programBookingChoice;

        Scanner scan = new Scanner(System.in);
        System.out.println("Would you like to board a petite floor sized plane or a grande floor sized plane?");
        planeSizeChoice = scan.next().charAt(0);
        planeSizeChoice = Character.toUpperCase(planeSizeChoice);
        if (planeSizeChoice == 'P') {
            floorType = new PetiteFloorGrid();
            floorType.initialiseFloorGrid();
            System.out.println("Would you like to be in the middle, window or asile?");
            seatingArea= scan.next().charAt(0);
            seatingArea = Character.toUpperCase(seatingArea);
            System.out.println("Would you like to be seated in first class or middle class?");
            seatEconomyOrFirst = scan.next().charAt(0);
            seatEconomyOrFirst = Character.toUpperCase(seatingArea);
            System.out.println("Would you like your seat to be booked via the smart program or the way program?");
            programBookingChoice = scan.next().charAt(0);
            programBookingChoice = Character.toUpperCase(programBookingChoice);
            if (seatEconomyOrFirst == 'F') {
                    if (programBookingChoice == 'S') {
                        customerSeat =  aTrainSmart.reserveFirstClass(planeSizeChoice, SeatType.MIDDLE);
                        System.out.println(floorType);
                    }
                    else {
                        customerSeat =  aTrainWay.reserveFirstClass(planeSizeChoice, SeatType.MIDDLE);
                        System.out.println(floorType);
                    }
                }
            }
        else {
            floorType = new GrandeFloorGrid();
            floorType.initialiseFloorGrid();
            System.out.println("Would you like to be in the middle, window or asile?");
            seatingArea= scan.next().charAt(0);
            seatingArea = Character.toUpperCase(seatingArea);
            System.out.println("Would you like to be seated in first class or middle class?");
            seatEconomyOrFirst = scan.next().charAt(0);
            seatEconomyOrFirst = Character.toUpperCase(seatingArea);
            System.out.println("Would you like your seat to be booked via the smart program or the way program?");
            programBookingChoice = scan.next().charAt(0);
            programBookingChoice = Character.toUpperCase(programBookingChoice);
            System.out.println("Did not reach start of if");//testing program LINE57
            if (seatEconomyOrFirst == 'F') {
                if (programBookingChoice == 'S') {
                    customerSeat =  aTrainSmart.reserveFirstClass(planeSizeChoice, SeatType.MIDDLE);
                    System.out.println(floorType);
                }
                else {
                    customerSeat =  aTrainWay.reserveFirstClass(planeSizeChoice, SeatType.MIDDLE);
                    System.out.println(floorType);
                }
                //System.out.println("Did not go through either if or else");//testing program
            }
        }

    }
}

我还有 2 行完全相同(但保存到不同的变量),它们工作得很好。

这个

seatEconomyOrFirst = scan.next().charAt(0);
seatEconomyOrFirst = Character.toUpperCase(seatingArea);

应该

seatEconomyOrFirst = scan.next().charAt(0);
seatEconomyOrFirst = Character.toUpperCase(seatEconomyOrFirst);

您忽略了读取字符并重新使用了seatingArea 这种将字符更新为大写的方法似乎是一种反模式。 你可以在一行中完成。 喜欢,

seatEconomyOrFirst = Character.toUpperCase(scan.next().charAt(0));

暂无
暂无

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

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