user2347481
user2347481

Reputation:

limited number of items to display in an array in java

anyone has any idea how can I limit this program to print only 5 of the 20 elements of the array.

Greetings and thanks.

import java.util.*;

public class lot {
    public static void main(String[] args) {
        int n[] = {1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

        Random rd = new Random();
        for (int i = 0; i < n.length; i++) {
            System.out.println(rd.nextInt(n[i]) + 1);
        }
    }

}

Upvotes: 1

Views: 119

Answers (1)

rush2sk8
rush2sk8

Reputation: 392

    import java.util.*;

public class Lot {

public static void main(String[] args) {

        int n[] = {1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

        Random rd = new Random();

        for (int i = 0; i < 5; i++) {

            System.out.println(rd.nextInt(n[i]) + 1);
        }
    }

}

Learn how to use a for loop http://en.wikipedia.org/wiki/For_loop#Java

Upvotes: 1

Related Questions