Answer:
public int add(Scanner input) {
int sum = input.nextInt();
if (input.hasNextInt()) {
sum=sum+add(input);
}
return sum;
}
Step-by-step explanation:
- Define a method named add that receives a reference to a Scanner object.
- Take sum as input from the user.
- Use if statement to check whether it has an integer.
- Add that integer to sum variable.
- Return the sum variable.