编写了一个程序来验证一排相邻的座位数量。座位要么已预订,要么可用,用 0 或 1 表示。该程序在大多数情况下都有效。如果一排有所需数量的座位可用,它将输出一条消息说它。问题是当所需的座位数量不可用或超过 6 个时。我该如何解决这个问题?
package javaapplication2;
import java.util.*;
public class JavaApplication2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount of people in your group, up to 6");
int num = input.nextInt();
int highest = num - 1;
String available = "";
String booking = " ";
int[] RowA = {0,0,1,0,0,0,1,0,0,1};
for (int i = 0; i < RowA.length; i++) {
if (RowA[i] == 0) {
available = available + (i + 1);
}
if (available.length() > booking.length()) {
booking = available;
}else if (RowA[i] == 1) {
available = "";
}
}
char low = booking.charAt(0);
char high = booking.charAt(highest);
if (num <= booking.length()) {
System.out.println("There are seats from " + low + " - " + high + ".");
System.out.println(booking);
}
else {
System.out.println("Sorry, the desired seat amount is not available. The maximum amount on Row is " + booking.length());
}
}
}