標題:
例外處理 (三)
[打印本頁]
作者:
tonyh
時間:
2013-3-9 17:07
標題:
例外處理 (三)
本帖最後由 tonyh 於 2013-3-14 18:46 編輯
針對不同的錯誤, 做不同的對應程序.
import java.util.Scanner;
public class ch61
{
public static void main(String args[])
{
try
{
Scanner s=new Scanner(System.in);
int a, b;
System.out.print("請輸入分子: ");
a=s.nextInt();
System.out.print("請輸入分母: ");
b=s.nextInt();
System.out.println(a+"/"+b+"="+(a/b));
}
catch(java.lang.ArithmeticException e)
{
System.out.println("發生運算錯誤! 分母不可為零!");
}
catch(java.util.InputMismatchException e)
{
System.out.println("發生格式錯誤! 輸入需為整數!");
}
catch(Exception e)
{
System.out.println("程式發生錯誤!");
System.out.println("例外類別: "+e.toString());
}
}
}
複製代碼
作者:
lon
時間:
2013-3-9 17:09
import java.util.*;
public class ch60{
public static void main(String args[]){
try{
int a,b;
Scanner s=new Scanner(System.in);
System.out.print("請輸入分子");
a=s.nextInt;
System.out.print("請輸入分母");
b=s.nextInt;
System.out.println(a+"/"+b+"="+(a/b));
}catch(Exception e){
System.out.println("error");
System.out.println(e.toString())
}catch(java.util.InputMismatchException e){
System.out.println("error");
System.out.println(e.toString())
}catch(java.lang.ArithmeticException e){
System.out.println("error");
System.out.println(e.toString())
}
}
}
複製代碼
作者:
黃博鴻
時間:
2013-3-9 17:13
import java.lang.Exception;
import java.util.Scanner;
public class ch61
{
public static void main(String args[])
{
try
{
int a,b;
Scanner s=new Scanner(System.in);
System.out.print("請輸入分子: ");
a=s.nextInt();
System.out.print("請輸入分母: ");
b=s.nextInt();
System.out.println(a+"/"+b+"="+(a/b));
}
catch(java.util.InputMismatchException e)
{
System.out.println("格式錯誤!數字應為整數");
System.out.println("例外類別: "+e.toString());
}
catch(java.lang.ArithmeticException e)
{
System.out.println("運算錯誤!分母不為零");
}
catch(Exception e)
{
System.out.println("程式發生錯誤!");
System.out.println("例外類別: "+e.toString());
}
}
}
複製代碼
作者:
t2364705
時間:
2013-3-9 17:16
import java.lang.Exception;
import java.util.Scanner;
public class ch61
{
public static void main(String args[])
{
try
{
int a,b;
Scanner s=new Scanner(System.in);
System.out.print("請輸入分子: ");
a=s.nextInt();
System.out.print("請輸入分母: ");
b=s.nextInt();
System.out.println(a+"/"+b+"="+(a/b));
}
catch(java.util.InputMismatchException e)
{
System.out.println("格式錯誤!數字應為整數");
System.out.println("例外類別: "+e.toString());
}
catch(java.lang.ArithmeticException e)
{
System.out.println("運算錯誤!分母不為零");
}
catch(Exception e)
{
System.out.println("程式發生錯誤!");
System.out.println("例外類別: "+e.toString());
}
}
}
複製代碼
作者:
尤泓鈞
時間:
2013-3-9 17:17
import java.lang.Exception;
import java.util.Scanner;
public class ch61
{
public static void main(String args[])
{
try
{
int a,b;
Scanner s=new Scanner(System.in);
System.out.print("請輸入分子: ");
a=s.nextInt();
System.out.print("請輸入分母: ");
b=s.nextInt();
System.out.println(a+"/"+b+"="+(a/b));
}
catch(java.util.InputMismatchException e)
{
System.out.println("格式錯誤!數字應為整數");
System.out.println("例外類別: "+e.toString());
}
catch(java.lang.ArithmeticException e)
{
System.out.println("運算錯誤!分母不為零");
}
catch(Exception e)
{
System.out.println("程式發生錯誤!");
System.out.println("例外類別: "+e.toString());
}
}
}
複製代碼
作者:
t3742238
時間:
2013-3-9 17:21
import java.lang.Exception;
import java.util.Scanner;
public class ch61
{
public static void main(String args[])
{
try
{
int a,b;
Scanner s=new Scanner(System.in);
System.out.print("請輸入分子");
a=s.nextInt;
System.out.print("請輸入分母");
b=s.nextInt;
System.out.println(a+"/"+b+"="+(a/b));
}
catch(java.util.InputMismatchExeption e)
{
System.out.println("格式錯誤!請輸入整數!!");
}
catch(java.lang.ArithmeticException e)
{
System.out.println("運算錯誤!分母不可為零!!");
}
catch(Exception e)
{
System.out.println("程式發生錯誤");
System.out.println(e.toString)
}
}
}
複製代碼
作者:
蔡昀佑
時間:
2013-3-9 17:24
import java.util.*;
public class ch60{
public static void main(String args[]){
try{
int a,b;
Scanner s=new Scanner(System.in);
System.out.print("請輸入分子");
a=s.nextInt;
System.out.print("請輸入分母");
b=s.nextInt;
System.out.println(a+"/"+b+"="+(a/b));
}catch(Exception e){
System.out.println("error");
System.out.println(e.toString())
}catch(java.util.InputMismatchException e){
System.out.println("error");
System.out.println(e.toString())
}catch(java.lang.ArithmeticException e){
System.out.println("error");
System.out.println(e.toString())
}
}
}
複製代碼
作者:
劉漢文
時間:
2013-3-14 18:51
import java.lang.Exception;
import java.util.Scanner;
public class ch61
{
public static void main(String args[])
{
try
{
int a,b;
Scanner s=new Scanner(System.in);
System.out.print("請輸入分子: ");
a=s.nextInt();
System.out.print("請輸入分母: ");
b=s.nextInt();
System.out.println(a+"/"+b+"="+(a/b));
}
catch(java.util.InputMismatchException e)
{
System.out.println("格式錯誤!數字應為整數");
System.out.println("例外類別: "+e.toString());
}
catch(java.lang.ArithmeticException e)
{
System.out.println("運算錯誤!分母不為零");
}
catch(Exception e)
{
System.out.println("程式發生錯誤!");
System.out.println("例外類別: "+e.toString());
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2