-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.

2

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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?
I edited my answer with the full code... how this helps you understanding how it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.