The homework problem is to write a while loop that prompts the user to enter as many integers as they want and to type "q" when done, then print the sum of those integers and the number of entries.
This is what I have so far... It doesn't work if the user inputs more than one integer and I don't know why.
package chapter06lab;
import java.util.Scanner;
public class ProgramF5
{
public static void main(String[] args)
{
Scanner one = new Scanner(System.in);
System.out.println("Please enter as many valid integers as you wish ('q' to finish): ");
int a = one.nextInt();
int count = 0;
int sum = 0;
while(one.hasNextInt())
{
count++;
sum =+ a;
}
System.out.println("The number of entries was: " + count);
System.out.println("The sum of all numbers entered is: " + sum);
}
}
Try this: