how do i ask the user to input integer up to 50 times, and add them all together at the end of the code in eclipse?

Viewed 25

Write a small sequence of statements that gets 50 integer inputs from the user, adds them together, and prints out their total. Print input prompts for the user. Properly use a counting loop, and your counting variable must be named ’inputCount’.

so far i only have this, i don't know how to do the rest

    Scanner in = new Scanner (System.in);
    
    System.out.println("Please enter an integer: ");

    int inputCount = in.nextInt();
1 Answers

You could create a for i<50;i++ loop or a while loop, put your integers in an array and afterwards iterate through it and add it all together.

I would recommend to add it in the first place in your loop, but as i understand your question you wanna do it after you received all 50.

I also would put the whole thing in a try except, otherwise you will have problems as somebody does not type in a valid int.

int iadded = 0;
int [] iarray = new int[50];

for (int i=0; i<50; i++){
   System.out.println("Please enter the " + i+1 + ". integer: ");
   iarray[i]=in.nextInt();
}

for (int i=0; i<50, i++){
   iadded += iarray[i];
}

System.out.println("All inputs added: " + iadded);
Related