我有一个项目,我需要输入 -25 到 25 之间的数字并计算每个输入的次数。我的代码只接受正数。第 26 行和第 39 行给了我一些问题,我无法让我的数量数组保存任何负数。
我的代码:
public class PP62{
public static void main(String[] args) {
char response = 'a';
int numbers[] = new int[100], count, amount[] = new int[100], n2;
count = 0;
do{
if((response == 'y')||(response == 'Y')||(count == 0)){
count++;
System.out.println("Enter in an integer between -25 and 25.");
do{
numbers[count] = SavitchIn.readLineInt();
if((numbers[count] > 25)||(numbers[count] < -25)){
System.out.println("Error, Invalid Input. Re-Enter integer between -25 and 25.");
}
else if((numbers[count] <= 25)||(numbers[count] >= -25)){
n2 = numbers[count];
amount[n2]++;
}
}while((numbers[count] > 25)||(numbers[count] < -25));
System.out.println("Enter in another integer? (Y/N)");
response = SavitchIn.readLineNonwhiteChar();
}
else if ((response == 'n')||(response == 'N')){
for(int a = -25; a <= 25; a++){
System.out.println(a + "'s entered: ");
System.out.println(amount[a] + "\n");
}
System.exit(0);
}
else{
System.out.println("Incorrect Input. Must be 'Y' (yes) or 'N' (no).");
response = SavitchIn.readLineNonwhiteChar();
}}while(count > 0);
}
}