在 Java 中执行 toUpperCase 方法后程序随机终止
Program randomly terminates after executing toUpperCase method in Java
我的 Java 程序在使用 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));
我的 Java 程序在使用 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));