//Tells how many numbers are positive and negative. Plus prints total.
//Prepared by Ethan Glover
import java.util.Scanner;
public class Prog4i1 {
public static void main(String[] args) {
//Ask for user input
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer, the input ends if it is 0: ");
//Set variables
int total = 0;
double average = 0.0;
int numOfPos = 0;
int numOfNeg = 0;
int numOfNum = 0;
int value;
//Calcuate numbers
do {
value = scan.nextInt();
if (value > 0) {
numOfPos++;
numOfNum++;
total += value;
}
else {
numOfNeg++;
numOfNum++;
total += value;
} while (value != 0)
}
//Calculate average
average = total/(numOfNum);
//Display output
if (total > 0) {
System.out.println("The number of positives is " + numOfPos);
System.out.println("The number of negatives is " + numOfNeg);
System.out.println("The total is " + total);
System.out.println("The average is " + average);
}
else {
System.out.println("No numbers are entered except 0");
}
}
}
Prog4i1.java:33: error: illegal start of expression } ^ Prog4i1.java:48: error: while expected } ^ Prog4i1.java:49: error: reached end of file while parsing } ^
Enter an integer, the input ends if it is 0: ex. 1 2 -1 3 0 The number of positives is 3 ... negatives is 1 ... total is 5 ... average is 1.25 -or- Enter an integer, the input ends if it is 0: ex. 0 No numbers are entered except 0
This post has been edited by thorax232: 13 October 2012 - 06:48 PM

New Topic/Question
Reply



MultiQuote




|