當int
數組不需要計數到10時。我的問題是charat
讀數爲10,因爲它們是兩個不同的字符。我如何能夠爲10例外?例如下面的程序,當您鍵入5個人時,程序從6-1打印出來,因爲1正在從第一個字符10開始讀取。當輸入6時,charat
正在讀取0,因此它會打印6-0。Java將10讀爲2個字符,如何更改此
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();
if (num > 6) {
System.out.println("You have exceeded the maximum amount of people allowed.");
}
int highest = num - 1;
String available = "";
String booking = " ";
int[] RowA = {0, 0, 1, 1, 1, 0, 0, 0, 0, 0};
for (int i = 0; i < RowA.length; i++) {
if (RowA[i] == 0) {
available = available + (i + 1);
}
if (available.length() > booking.length()) {
booking = available;
System.out.println(booking);
} else if (RowA[i] == 1) {
available = "";
}
}
if (num <= booking.length()) {
char low = booking.charAt(0);
char high = booking.charAt(highest);
System.out.println("There are seats from " + low + " - " + high + ".");
} else {
System.out.println("The desired seat amount is not available. The maximum amount on Row is " + booking.length());
}
}
}
變量'booking'代表什麼?這聽起來像你正在嘗試使用字符串的字符作爲數字。也許你應該使用'int []'而不是String。 –
您應該重新考慮您設計代碼的方式。我不會使用String作爲集合,因爲您會遇到各種問題。我還會檢查你的「預訂」循環工作,因爲我可以看到它的一些問題。如果你不知道你想添加多少元素,你可以使用'int []',但是'List'會更簡單。 –