114-1程設B組Week8實習課小考第一題


Submit solution

Points: 50 (partial)
Time limit: 10.0s
Memory limit: 977M

Authors:
Problem type
Allowed languages
Java 19, Java 8

題目說明

  • 輸入多個整數,以0作為結束。
  • 請分別計算這些輸入的數值(不含0)中所含正數個數、負數個數、總和及平均值。

測資範圍

\[ -10 <= 輸入的整數 <= 10\]


提醒

  • 平均值變數以double宣告。
  • 平均值統一四捨五入到整數,使用Math.round()
  • 冒號:為英文半形。
  • 最後一行要用System.out.println()才會跟範例輸出一樣。
  • 程式前面記得加上import java.util.Scanner;
  • 輸入輸出請參考範例。

範例輸入/輸出

範例輸入1

-3
8
-10
6
9
0

範例輸出1

正數個數:3
負數個數:2
總和:10
平均值:2.0

範例輸入2

0

範例輸出2

正數個數:0
負數個數:0
總和:0
平均值:0.0

範例輸入3

-9
5
-7
1
-5
0

範例輸出3

正數個數:2
負數個數:3
總和:-15
平均值:-3.0

Comments


  • 0
    scu14156258  commented on Nov. 5, 2025, 9:30 p.m.

    import java.util.Scanner; public class a2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i = 0; int p = 0; int b = 0; int n = 0; int r = 0; int a=0;

        do {
            a = scanner.nextInt();
            n++;
            r += a;
            if (a > 0) {
                p++;
            }
            if (a < 0) {
                b++;
            }
        } while (a != 0);
    
        if(r!=0){
            System.out.println("正數個數:"+p);
            System.out.println("負數個數:"+b);
            System.out.println("總和:"+r);
        double k=r /(n-1);
         long g=Math.round(k);
        System.out.println("平均值"+(double)g);}
        if(r==0){
            System.out.println("正數個數:"+p);
            System.out.println("負數個數:"+b);
            System.out.println("總和:"+r);
    
            System.out.println("平均值:"+0.0);}
    
    
    
    }

    }