无法在数组中保留负值

时间:2013-10-17 01:04:26

标签: java arrays negative-number

我有一个项目,我需要在-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);
    }   
}

3 个答案:

答案 0 :(得分:0)

在您的代码中

else if((numbers[count] <= 25)||(numbers[count] >= -25)){
                n2 = numbers[count];
                amount[n2]++;
            }

如果n2 = numbers[count] = -25怎么办? n2将为负数,amount[n2]++将使用负索引访问数组。您无法访问具有负索引的数组,这样做会导致java.lang.ArrayIndexOutOfBoundsException

为什么我们不使用大小为50且数量为a[n2+25]++的数组呢!

答案 1 :(得分:0)

java中的数组索引不能为负数,因此需要从0开始修复它们

像这样:

char response = 0;
int numbers[] = new int[51];

do
{
    System.out.println("Enter in an integer between -25 and 25.");
    int n = SavitchIn.readLineInt();
    if( (n > 25) || (n < -25))
        System.out.println("Error, Invalid Input. Re-Enter integer between -25 and 25.");
    else
        numbers[n+25]++;

    System.out.println("Enter in another integer? (Y/N)");
    do
    {
        response = SavitchIn.readLineNonwhiteChar();
        if ( (Character.toLowerCase(response) != 'y') && (Character.toLowerCase(response) != 'n') )
            System.out.println("Incorrect Input. Must be 'Y' (yes) or 'N' (no).");
    } while ( (Character.toLowerCase(response) != 'y') && (Character.toLowerCase(response) != 'n') );
} while (Character.toLowerCase(response) == 'y');

for(int a = -25; a <= 25; a++)
    System.out.println(a + "'s entered: " + numbers[25+a] + " times\n");

答案 2 :(得分:-1)

我无法测试代码,因为我没有SavitchIn类,但我注意到了这行

amount[n2]++;

在某些时候会尝试使用负数索引数组。数组索引从0开始。我会推荐

amount[n2+25]++;

然后

System.out.println(amount[a-25]+"\n");

在显示屏上。也就是说,将要表示的数字(-25..25)移动到数组可以处理的范围(0..50)。