Counting positive and negative number and computing the average of numbers

雨点打透心脏的1/2处 2022-08-09 02:19 278阅读 0赞
  1. 代码:
  2. import java.util.Scanner;
  3. public class Ten_six {
  4. public static void main(String[] args) {
  5. System.out
  6. .println("Enter an int value, the progrem exits if the input is 0: ");
  7. Scanner cin = new Scanner(System.in);
  8. int a = 1, s = 0, s1 = 0, s2 = 0;
  9. double num = 1;
  10. for (int i = 0; a != 0; ++i) {
  11. a = cin.nextInt();
  12. s += a;
  13. if (a > 0)
  14. s1++;
  15. if (a < 0)
  16. s2++;
  17. num = i;
  18. }
  19. System.out.println("The number of positives is " + s1);
  20. System.out.println("The number of negatives is " + s2);
  21. System.out.println("The total is " + s);
  22. System.out.println("The average is " + (s / num));
  23. }
  24. }
  25. 运行结果:

Center

发表评论

表情:
评论列表 (有 0 条评论,278人围观)

还没有评论,来说两句吧...

相关阅读