-4

hi guys new to programming and to stack overflow. I was wondering if i could get a few pointers, so basically i've made an array holding values of 1-200. But now i would like to be able to ask the user for input and then add together the remaining numbers in the array.

e.g. user enters 100 - so then all numbers from 100 - 200 are added together and then the total outputted.

I have a feeling it will be something really simple I just don't know where to start.

Thanks Guys.

bigbeans
  • 9
  • 4

2 Answers2

3

I just don't know where to start.

  1. Write a HelloWorld programm to learn how to output something.
  2. Write a program which reads some input values, in different datatypes.
  3. Learn how for-loops work.
  4. Put all your knowledge together.
kai
  • 6,702
  • 22
  • 38
2

You can use a the Scanner s = new Scanner(System.in); to ask an input from the user...

Then simply use a for loop starting from the inputted value to the end of the array and sum the results..

int sum = 0;
for (int i = inputtedValue ; i < 200 ; i++)
{
   sum += array[i];
}

System.out.println(sum);

EDIT

For better understanding for the OP I am editing my answer posting the complete code, although it is not how stackoverflow works.

public static void main(String [] args)
{
    Scanner s = new Scanner(System.in);
    int inputtedValue = s.nextInt();
    s.nextLine();
    int[] array = new int[200];
    for (int i = 0 ; i < 200 ; i++)
    {
       array[i] = i+1;
    }

    int sum = 0;
    for (int i = inputtedValue ; i < 200 ; i++)
    {
       sum += array[i];
    }

    System.out.println(sum);
}

input: 199 output: 200

input: 100 output: 15050

input: 0 output: 20100

Marcx
  • 6,806
  • 5
  • 46
  • 69
  • Thank you for your quick reply. i have used to above code which you suggested and it now makes sense to me :) but the sum is outputting at 0 every time. have i done something wrong? – bigbeans Jan 20 '16 at 13:36
  • I edited my answer with the full code... how this helps you understanding how it works. – Marcx Jan 20 '16 at 17:14