Answer:
import java.util.*;
class Main
{
public static void main(String[] args)
{
System.out.println("Enter integers and 0 to exit");
Scanner a1=new Scanner(System.in);
System.out.println(add(a1));
}
public static int add(Scanner a1)
{
int total = a1.nextInt();
if (a1.hasNextInt())
{
total =total +add(a1);
}
return total;
}
}
Explanation:
The only thing that needs explanation here is hasnextInt. This returns true if entered number is integer and false if entered is not an integer. And rest is as shown in the program.