アレイの複数の値を減らします

Wolfizzy:

私は、私が搭乗し、目的地のポイントをユーザーに依頼する必要があるの予約システムを作ってるんです。そこ6局である、と私は搭乗と宛先ポイントとこれらの間のステーションのすべてからの乗客数を減算します。

TrainService.java クラス:

private String[] stations = {"Lorem", "Dolor", "Sit", "Amet", "Consectetur"};
private int[] numOfFirstClassSeats = {48, 48, 48, 48, 48};

public int[] getNumOfFirstClassSeats() {
    return this.numOfFirstClassSeats;
}

public void setNumOfFirstClassSeats(int[] numOfFirstClassSeats) {
    this.numOfFirstClassSeats = numOfFirstClassSeats;
}

public void printStations() {
    System.out.println("\n~ AVAILABLE SEATS ~");
    System.out.println("-------------------\n");

    System.out.printf("%-20s %-20s %-21s %-23s %-20s\n", "DEPARTING", "ARRIVING", "FIRST CLASS", "STANDARD CLASS", "EXCURSION CLASS");
    System.out.println("-------------------------------------------------------------------------------------------------------");
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[0], stations[1], getNumOfFirstClassSeats()[0]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[1], stations[2], getNumOfFirstClassSeats()[1]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[2], stations[3], getNumOfFirstClassSeats()[2]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[3], stations[4], getNumOfFirstClassSeats()[3]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[4], stations[5], getNumOfFirstClassSeats()[4]);
}

BookingSystem.java クラス:

// Prompt user for the boarding point.
System.out.println("\n0. Lorem");
System.out.println("1. Ipsum");
System.out.println("2. Dolor");
System.out.println("3. Sit");
System.out.println("4. Amet\n");

System.out.print("Select a boarding point: ");
String boardingPoint = sc.nextLine();
int boardingPointNum = Integer.parseInt(boardingPoint);

// Prompt user for the destination point.
System.out.println("\n1. Ipsum");
System.out.println("2. Dolor");
System.out.println("3. Sit");
System.out.println("4. Amet");
System.out.println("5. Consectetur\n");

System.out.print("Select a destination point: ");
String destinationPoint = sc.nextLine();
int destinationPointNum = Integer.parseInt(destinationPoint);

// Reduce the number of available seats to the respective stations.
if (boardingPoint.equals("0") && destinationPoint.equals("1")) {
  numOfFirstClassSeats[0] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("2")) {
  numOfFirstClassSeats[0-1] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("3")) {
  numOfFirstClassSeats[0-2] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("4")) {
  numOfFirstClassSeats[0-3] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("5")) {
  numOfFirstClassSeats[0-4] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
}

// The same goes for the other stations
...

else {
  System.out.println("ERROR: Invalid input!");
  return;
}

問題:(搭乗と目的地のポイントをチェックするこの方法でBookingSystem.javaファイル)乱雑と繰り返しています。例えば使用して、他のクリーナーと同じことをやっての効率的な方法があり、forループは?ありがとう!

akuzminykh:

はい、あなたがあなたのコードを簡素化することができますためのステートメント。コードはそれのために叫んされて2つの場所があります。


    System.out.printf("%-20s %-20s %-21s %-23s %-20s\n", "DEPARTING", "ARRIVING", "FIRST CLASS", "STANDARD CLASS", "EXCURSION CLASS");
    System.out.println("-------------------------------------------------------------------------------------------------------");
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[0], stations[1], getNumOfFirstClassSeats()[0]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[1], stations[2], getNumOfFirstClassSeats()[1]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[2], stations[3], getNumOfFirstClassSeats()[2]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[3], stations[4], getNumOfFirstClassSeats()[3]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[4], stations[5], getNumOfFirstClassSeats()[4]);

このパートでは、あなたが行ずつ増加指数との配列から値を取得していることがわかります。ただ、使用のための声明。

String pattern = "%-20s %-20s %-21s %-23s %-20s\n";
System.out.printf(pattern, "DEPARTING", "ARRIVING", "FIRST CLASS", "STANDARD CLASS", "EXCURSION CLASS");
System.out.println("-------------------------------------------------------------------------------------------------------");
for (int i = 0; i < 5; i++) {
    System.out.format(pattern, stations[i], stations[i + 1], getNumOfFirstClassSeats()[i]);
}

// Reduce the number of available seats to the respective stations.
if (boardingPoint.equals("0") && destinationPoint.equals("1")) {
  numOfFirstClassSeats[0] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("2")) {
  numOfFirstClassSeats[0-1] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("3")) {
  numOfFirstClassSeats[0-2] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("4")) {
  numOfFirstClassSeats[0-3] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("5")) {
  numOfFirstClassSeats[0-4] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
}

これは、他の場所です。同じ話、文によって文を増加数。

for (int i = 0; i < 5; i++) {
    if (boardingPoint.equals("0") && destinationPoint.equals(Integer.toString(i + 1))) {
        numOfFirstClassSeats[0 - i] -= numOfFirstClassAdults;
        stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
        break;
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=341577&siteId=1