2

I need help creating an array that counts up to a given number. The output should look something like this:

Enter a positive integer: 8
Counting up: 1 2 3 4 5 6 7 8
Counting down: 8 7 6 5 4 3 2 1
The first 8 multiples of 5: 5 10 15 20 25 30 35 40
The first 8 multiples of 10: 10 20 30 40 50 60 70 80

Here is what I have so far:

Scanner input = new Scanner(System.in);

    int[] myList = new int[1];

    System.out.print("Enter a positive integer: ");
    promptUser(myList);

    int[] testArray = { 1, 1, 2, 3, 5, 8, 13 };
    System.out.print("Test array: ");
    printArray(testArray);

    System.out.print("Counting up: ");
    int[] countingUp = countUp(n);
    printArray(countingUp);
}

public static void promptUser(int[] a){
    Scanner input = new Scanner(System.in);
    for(int i=0; i<a.length; i++){
        a[i] = input.nextInt();

    }
}

public static void printArray(int[] array){
    for(int i=0; i<array.length; i++)
        System.out.print(array[i]);

    }

public static int[] countUp(int n){
    for(int i=0; i<n; i++){
        int count = 0;
        while(count<n){
            count++;
        }
    }
}
}

Everything seems to work alright except for the last method called countingUp.

Thank you so much!

Jordon
  • 91
  • 1
  • 9

4 Answers4

1
   public static int[] countUp(int n){
        for(int i=0; i<n; i++){
            int count = 0;
            while(count<n){
                count++;
            }
        }
    }

change this to

public static int[] countUp(int n){
        int [] temp=new int[n];
        for(int i=0; i<n; i++){
         temp[i]=i+1;
        }
     return temp;
    }



System.out.print("Counting up: ");
    int[] countingUp = countUp(n);
    printArray(countingUp);

In this line change to

 int[] countingUp = countUp(n);
 for(int i=0;i<countingUp.length;i++){
   system.out.println(countingUp[i]+" ");
}
0

We can start by extracting the common logic of counting by providing an initial message, the number of times to run, an initial value and an increment. Something like,

private static void count(String msg, int times, int init, int inc) {
    System.out.print(msg);
    for (int i = 0; i < times; i++) {
        System.out.print(' ');
        System.out.print(init);
        init += inc;
    }
    System.out.println();
}

We can then implement the entirety of the requirements with something like

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a positive integer: ");
    System.out.flush();
    do {
        int num = scanner.nextInt();
        count("Counting up:", num, 1, 1);
        count("Counting down:", num, num, -1);
        count(String.format("The first %d multiples of 5:", num), num, 5, 5);
        count(String.format("The first %d multiples of 10:", num), num, 10, 10);
        System.out.print("Enter a positive integer: ");
        System.out.flush();
    } while (scanner.hasNextInt());
}

This will produce the requested output given an input of 8, and will then prompt for more input.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

First of all, If you are trying to change the Array Size Dynamically then It is NOT POSSIBLE in java Take a look @ this accepted answer. I recommend you to use ARRAYLIST instead.

Although I have found below mistakes in your code. In your code I do not understand two things.

First One:

System.out.print("Counting up: "); int[] countingUp = countUp(n); printArray(countingUp);

What is the value of n? I think it is not being initialized.

Second One:

public static int[] countUp(int n){ for(int i=0; i<n; i++){ int count = 0; while(count<n){ count++; } } }

What will return this function? You have not returned anything from it.

Community
  • 1
  • 1
Maulik Dodia
  • 1,629
  • 2
  • 21
  • 48
0

Apparently you don't need an array just follow below steps.

First create a class which handle all your calculating and counting

class SupportCounting {

public void countUp(int num) {
    System.out.print("Counting up : ");
    for (int i = 1; i <= num; i++) {
        System.out.print(i);
        System.out.print(" ");
    }
    System.out.println("");
}

public void countDown(int num) {
    System.out.print("Counting Down : ");
    for (int i = num; i > 0; i--) {
        System.out.print(i);
        System.out.print(" ");
    }
    System.out.println("");
}

public void printMultiple(int num, int scalefactor) {
    System.out.print("First " + num + " multiples of " + scalefactor + " : ");
    for (int i = 1; i <= num; i++) {
        System.out.print(i * scalefactor);
        System.out.print(" ");
    }
    System.out.println("");
}}

Then make use of that class in you main method

public class Counting {

public static void main(String[] args) {

    Scanner reader = new Scanner(System.in);
    System.out.print("Enter a positive integer : ");
    int n = reader.nextInt();

    SupportCounting sc = new SupportCounting();
    sc.countUp(n);
    sc.countDown(n);
    sc.printMultiple(n, 5);
    sc.printMultiple(n, 10);
}}

Hope that helps

Thush-Fdo
  • 506
  • 1
  • 9
  • 28