简体   繁体   中英

Java Array of odd numbers

So I just started a class and am starting to learn java. Ive run into an assignment where you have to take the odds between -6 and 38 and put them into an array. I am completely lost and this is probably a stupid question but where am I going wrong in the program I started?

import java.util.*;



public class oddNumbersClass {
    public static void main(String args[]) {
        int lowNumber = -6;
        int highNumber = 38;
        int arraySize = (highNumber - lowNumber) / 2 + 1;
        int[] odds = new int[arraySize];

        for (int i = 0; i < highNumber - lowNumber; i += 2) {
            if (lowNumber % 2 == 0) {
                lowNumber += 1;
                odds[i] = lowNumber;
            }
        }

            System.out.println("Odd number of numbers between " + lowNumber + " and " + highNumber + " is " + arraySize);
            for (int counter = 1; counter <= arraySize; counter++) {
                System.out.print(odds[counter - 1] + " ");
            }

        }
    }

The output I get is:

Odd number of numbers between -5 and 38 is 23
-5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Process finished with exit code 0

Im guessing because of the

        if (lowNumber % 2 == 0) {
            lowNumber += 1;
            odds[i] = lowNumber;

Your loop should start from lowNumber and end at highNumber. And you should check for odds in them.

Loop should be like:

int j = 0;
for(int i = lowNumber; i < highNumber; i++){
    if(i % 2 == 1)
        odds[j++] = i;
}

Problems with your code are many.

  1. Your loop start and end-points are wrong.

  2. You are incrementing i by 2. (Why?)

  3. Check indexing of your odds[] array when you are initializing it.

Your code isn't working because you only add the value to the array when lowNumber%2==0 . This is only true once because after that, you increment by 2 and never enter the conditional. Try this instead:

for(int i=0; i<arraySize; i++) {
    if (lowNumber % 2 == 0) {
        lowNumber += 1;
    }
    odds[i] = lowNumber;
    lowNumber += 2;
}

As you are just starting out, you wouldn't be expected to use or know about ArrayList yet, but they would make this problem very easy.

import java.util.*;
public class oddNumbersClass {
    public static void main(String args[]) {
        int lowNumber = -6;
        int highNumber = 38;
        ArrayList<Integer> odds = new ArrayList<Integer>();
        for (int i = lowNumber; i <= highNumber; i ++) {
            if (lowNumber % 2 != 0) {
                odds.add(i);
            }
        }
        System.out.println("Odd number of numbers between " + lowNumber + " and " + highNumber + " is " + odds.size());
        for (Integer i : odds) {
            System.out.print(i + " ");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM