Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Javascript (https://www.askmehelpdesk.com/forumdisplay.php?f=452)
-   -   Array need help (https://www.askmehelpdesk.com/showthread.php?t=821845)

  • Feb 16, 2016, 10:58 PM
    iamjmael
    Array need help
    Write a program that will ask the user to enter a number and then place the 10 consecutive numbers in a single-dimensional array starting from the number entered by the user. The program will then display the elements inside index 2 and 4 and 6.
  • Feb 17, 2016, 02:51 AM
    kcomissiong
    If you need help, please post the particular area you are struggling with and what you have tried. If you want someone to do this for you, pay a programmer.
  • Feb 17, 2016, 03:03 AM
    iamjmael
    import java.util.*;
    public class SetB4
    {
    public static void main(String args[])
    {
    int[] a=new int[10];
    Scanner sc=new Scanner(System.in);
    System.out.println("Please enter elements...");
    for(int j=0;j<10;j++)
    a[j]=sc.nextInt();


    }
    }






    ------------------------------
    sorry kcomissiong :)
    this program work but the problem
    is I'm struggle to print the output...
    I try the
    System.out.println([2][4][5]);
    but it didn't work it say illegal statement :(
    need help
  • Feb 17, 2016, 08:46 AM
    CravenMorhead
    first off you are asking the user to put in 10 numbers, not put in 1 number and then populate the array. This might be nit picking though.

    It should be:

    system.out.println("Enter the number");
    int baseNumber = sc.nextInt();
    for(I=0;i<10;i++)a[i]=i+1+baseNumber;

    Printing it out you need to index the proper elements:

    printf(" Elements, 2,4,6 are: %d %d %d", a[2], a[4], a[6]);

    Figure out how to output variables and make sure you're indexing the proper place in the array. Google how if you're confused. That is how I, being a brilliant programmer, figure things like this out.
  • Feb 18, 2016, 01:57 AM
    iamjmael
    Thank you CravenMorhead :)

  • All times are GMT -7. The time now is 05:59 AM.