Board logo

標題: 例外處理 (一) [打印本頁]

作者: tonyh    時間: 2017-3-25 10:45     標題: 例外處理 (一)

本帖最後由 tonyh 於 2017-3-25 11:20 編輯

寫一個除法程式, 測試當分母為零, 以及輸入字母時, 產生的例外.

  1. import java.util.Scanner;
  2. public class Ch50
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         int x,y;
  7.         Scanner s=new Scanner(System.in);
  8.         System.out.print("請輸入分子: ");
  9.         x=s.nextInt();
  10.         System.out.print("請輸入分母: ");
  11.         y=s.nextInt();
  12.         System.out.println(x+"/"+y+" = "+x/y);
  13.     }
  14. }
複製代碼

作者: 陸長辰    時間: 2017-3-25 11:03

  1. import java.util.Scanner;
  2. public class Ch41
  3. {
  4.     public static void main(String args[])
  5.     {
  6.        Scanner s=new Scanner(System.in);
  7.        int x,y;
  8.        System.out.print("請輸入分子");
  9.        x=s.nextInt();
  10.        System.out.print("請輸入分母");
  11.        x=s.nextInt();
  12.        System.out.print(x+"/"+y+"="+x/y);
  13.     }
  14. }
複製代碼

作者: 黃茂勛    時間: 2017-3-25 11:03

  1. import java.util.Scanner;
  2. public class Ch48
  3. {
  4.     public static void main(String args[])
  5.     {
  6.        Scanner s=new Scanner(System.in);
  7.        int x,y;
  8.        System.out.print("請輸入分子: ");
  9.        x=s.nextInt();
  10.        System.out.print("請輸入分母: ");
  11.        y=s.nextInt();
  12.        System.out.print(x+"/"+y+" = "+x/y);
  13.     }
  14. }
複製代碼

作者: 陳泓瑜    時間: 2017-3-25 11:06

  1. import java.util.Scanner;
  2. public class Ch40
  3. {
  4.    public static void main(String args[])
  5.    {
  6.       float x,y;

  7.       Scanner s=new Scanner(System.in);
  8.       
  9.       System.out.print("ENTER X/-:");
  10.       x=s.nextFloat();
  11.       System.out.print("ENTER -/Y:");
  12.       y=s.nextFloat();
  13.       
  14.       System.out.println(x+"/"+y+"="+(x/y));
  15.    }
  16. }
複製代碼
[attach]2099[/attach]
作者: 林侑成    時間: 2017-3-25 11:44

  1. import java.util.Scanner;
  2. public class Ch50
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         int x,y;
  7.         Scanner s=new Scanner(System.in);
  8.         System.out.print("請輸入分子: ");
  9.         x=s.nextInt();
  10.         System.out.print("請輸入分母: ");
  11.         y=s.nextInt();
  12.         System.out.println(x+"/"+y+" = "+x/y);
  13.     }
  14. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2