返回列表 發帖
  1. import java.util.Scanner;

  2. public class JPA03 {
  3.         static Scanner keyboard = new Scanner(System.in);
  4.         static int i = -1;

  5.         public static void main(String[] args) {
  6.         int total = 0, s = 0;
  7.         do{
  8.                 System.out.print("請輸入消費金額,或輸入-1結束:");
  9.                 s=keyboard.nextInt();
  10.                 total+=s;
  11.         }while(s!=-1);
  12.         System.out.println("電腦週邊總消費:"+(total+1));
  13.     }
  14. }
複製代碼

TOP

  1. import java.util.Scanner;

  2. public class JPA04 {
  3.         static Scanner keyboard = new Scanner(System.in);

  4.         public static void main(String args[]) {
  5.                 int n = 0;
  6.                 System.out.print("Input n (0 <= n <=16):");
  7.                 n = keyboard.nextInt();
  8.                 while (n != 999) {
  9.                         System.out.println(n + "的階乘(尾端遞迴)=" + fac(n));
  10.                         System.out.println(n + "的階乘(迴圈)=" + facLoop(n, 1));
  11.                         System.out.print("Input n (0 <= n <=16):");
  12.                         n = keyboard.nextInt();
  13.                 }
  14.         }

  15.         static int fac(int n) {
  16.                 if (n == 0) {
  17.                         return 1;
  18.                 } else {
  19.                         return n * fac(n - 1);
  20.                 }

  21.         }

  22.         static int facLoop(int n, int m) {
  23.                 while (n != 0) {
  24.                         m = m * n;
  25.                         n--;
  26.                 }
  27.                 return m;
  28.         }

  29. }
複製代碼

TOP

返回列表