返回列表 發帖
  1. package test;
  2. import java.util.*;
  3. import java.util.Scanner;
  4. public class Test
  5. {
  6.     public static void main(String args[])
  7.     {
  8.          int x,y,n=0;
  9.          while(true)
  10.          {
  11.              if(n==3)
  12.              {
  13.                  System.out.println("錯誤嘗試過多! 程式跳出!");
  14.                  return;
  15.              }
  16.              try
  17.              {
  18.                  Scanner s=new Scanner(System.in);
  19.                  System.out.print("請輸入分子: ");
  20.                  x=s.nextInt();
  21.                  System.out.print("請輸入分母: ");
  22.                  y=s.nextInt();
  23.                  System.out.println(x+"/"+y+"="+(x/y));
  24.                  return;
  25.              }catch(ArithmeticException e)
  26.              {
  27.                  n++;
  28.                  System.out.println("運算錯誤! 分母不可為零!\n");
  29.              }catch(InputMismatchException e)
  30.              {
  31.                  n++;
  32.                  System.out.println("格式錯誤! 輸入須為整數!\n");
  33.              }
  34.          }
  35.     }
  36. }
複製代碼

TOP

  1. import java.util.InputMismatchException;
  2. import java.util.*;
  3. import java.util.Scanner;
  4. public class Test
  5. {
  6.     public static void main(String args[])
  7.     {
  8.          int x,y,n=0;
  9.          while(true)
  10.          {
  11.              if(n==3)
  12.              {
  13.                  System.out.println("錯誤嘗試過多! 程式跳出!");
  14.                  return;
  15.              }
  16.              try
  17.              {
  18.                  Scanner s=new Scanner(System.in);
  19.                  System.out.print("請輸入分子: ");
  20.                  x=s.nextInt();
  21.                  System.out.print("請輸入分母: ");
  22.                  y=s.nextInt();
  23.                  System.out.println(x+"/"+y+"="+(x/y));
  24.                  return;
  25.              }catch(ArithmeticException e)
  26.              {
  27.                  n++;
  28.                  System.out.println("運算錯誤! 分母不可為零!\n");
  29.              }catch(InputMismatchException e)
  30.              {
  31.                  n++;
  32.                  System.out.println("格式錯誤! 輸入須為整數!\n");
  33.              }
  34.          }
  35.     }
  36. }
複製代碼

TOP

返回列表