Counting positive and negative number and computing the average of numbers
代码:
import java.util.Scanner;
public class Ten_six {
public static void main(String[] args) {
System.out
.println("Enter an int value, the progrem exits if the input is 0: ");
Scanner cin = new Scanner(System.in);
int a = 1, s = 0, s1 = 0, s2 = 0;
double num = 1;
for (int i = 0; a != 0; ++i) {
a = cin.nextInt();
s += a;
if (a > 0)
s1++;
if (a < 0)
s2++;
num = i;
}
System.out.println("The number of positives is " + s1);
System.out.println("The number of negatives is " + s2);
System.out.println("The total is " + s);
System.out.println("The average is " + (s / num));
}
}
运行结果:
还没有评论,来说两句吧...