-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);
}
}