can someone help me with this while loop assignment
im doing an assignment for java intro class and am getting the results i need just dont know if this is layed out right or not
here is what were supposed to do
Write a program that will read an unspecified number of positive numbers from the keyboard and determine the sum and the average of these numbers. A negative number should be used to terminate the input, i.e. when the program encounters a negative number, it should stop prompting and reading data and at that point should output the results. Note that the negative number that terminates the looping should not be added to the sum and certainly should not be counted as one of the valid numbers.
A sample program run would look like this :
Enter a number that is positive or zero 3
Enter a number that is positive or zero 5
Enter a number that is positive or zero 6
Enter a number that is positive or zero 4
Enter a number that is positive or zero -1
You have entered 4 numbers
Total of the numbers you entered is 18
Average of the numbers is 4.5
The program should use a WHILE loop to accomplish it's mission.
when i run it it constantly keeps asking me to put in a number when i only want to enter about 4 or 5 numbers and and then calculate the sum and average of those #'s
btw this is what i got
Code java:
import java.util.Scanner ;
public class Assign6_Roberts{
public static void main (String [] args){
//Create Scanner
Scanner input = new Scanner(System.in);
int number = 0, sum = 0, numamount = 0; // You can change the variable names.
while(number >-1)
{ // Begin while statement
System.out.println("Enter a number that is positive or zero: "); // Get the next number
number = input.nextInt();
sum = sum+number; // Get the sum so far.
numamount++; // Get the number amount so far.
} // End while statement.
double average = sum/numamount; //Get the average.
System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers
System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers
System.out.println("Average of the numbers is " + average); // Output average of numbers.
}
}
Re: can someone help me with this while loop assignment
You have a number of errors, but not the error you are describing. I'm not going to spoon feed you the solution, but am willing to help you get there.
1. If the first number entered is a negative number, should the code in the while loop execute? What does your program do if the first number is negative?
2. When you are taking the input, why are you using a println? this causes the number the user types in to be on the next line from this string: "Enter a number that is positive or zero: "
3. If I enter any number of positive integers, and then enter a negative one to quit, should that negative number be ignored, or should it be added to sum? What does your code do? Step through it before answering this question
4. Same as #3, but this time, when a negative number is entered should numamount be incremented? What does your code do?
5. When calculating the average, what happens when you divide two ints? What will the output be? Will we potentially lose any information, if expecting a double?
Re: can someone help me with this while loop assignment
The reason you are entering more then 5 number is because the while loop will go until someone enters a number<1. Instead the while loop should check if numamount < 5. Then have an if statement check if the entered number<1.
Code Java:
public static void main(String[] args) {
// TODO code application logic here
//Create Scanner
Scanner input = new Scanner(System.in);
int number = 0, sum = 0, numamount = 0; // You can change the variable names.
while(numamount < 5)
{ // Begin while statement
System.out.println("Enter a number that is positive or zero: "); // Get the next number
number = input.nextInt();
if (number < 0){
System.out.println("you have Entered an invalid number, please enter again: ");
number = input.nextInt();
}
sum = sum+number; // Get the sum so far.
numamount++; // Get the number amount so far.
} // End while statement.
double average = sum/numamount; //Get the average.
System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers
System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers
System.out.println("Average of the numbers is " + average); // Output average of numbers.
}