溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java編程異常簡(jiǎn)單代碼示例

發(fā)布時(shí)間:2020-08-19 16:37:48 來(lái)源:腳本之家 閱讀:116 作者:Evan19870504 欄目:編程語(yǔ)言

練習(xí)1

寫(xiě)一個(gè)方法void triangle(int a,int b,int c),判斷三個(gè)參數(shù)是否能構(gòu)成一個(gè)三角形。如果不能則拋出異常IllegalArgumentException,顯示異常信息:a,b,c “不能構(gòu)成三角形”;如果可以構(gòu)成則顯示三角形三個(gè)邊長(zhǎng)。在主方法中得到命令行輸入的三個(gè)整數(shù),調(diào)用此方法,并捕獲異常。

兩邊之和大于第三邊:a+b>c
兩邊之差小于第三邊:c-a

package 異常;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestTriangle {
  public static void triangle(int a, int b,int c) throws IllegalArgumentException, InputMismatchException{
    int x[] = new int[3];
    x[0] = a;
    x[1] = b;
    x[2] = c;
    Arrays.sort(x);
    if ((x[0]+x[1]>x[2])&&(x[2]-x[1]<x[0]))
      System.out.println("三角形的三邊長(zhǎng)為:"+a+","+b+","+c);
    else
      throw new IllegalArgumentException();
  }
  public static void main(String[] args) {
    int a=0, b=0, c=0;
    Scanner in = new Scanner(System.in);
    System.out.println("請(qǐng)分別輸入三角形的三邊長(zhǎng):");
    try{
      a = in.nextInt();
      b = in.nextInt();
      c = in.nextInt();
      triangle(a, b, c);
    }catch(InputMismatchException e1){
      System.err.println("請(qǐng)輸入整數(shù)作為三角形的邊長(zhǎng)!");
      e1.printStackTrace();
    }catch(IllegalArgumentException e2){
      System.err.println(a+","+b+","+c+"不能構(gòu)成三角形");
    }
  }
}

Java編程異常簡(jiǎn)單代碼示例

Java編程異常簡(jiǎn)單代碼示例

練習(xí)2:

從命令行輸入5個(gè)整數(shù),放入一整型數(shù)組,然后打印輸出。要求:

如果輸入數(shù)據(jù)不為整數(shù),要捕獲輸入不匹配異常,顯示“請(qǐng)輸入整數(shù)”;如果輸入數(shù)據(jù)多余5個(gè),捕獲數(shù)組越界異常,顯示“請(qǐng)輸入5個(gè)整數(shù)”。

無(wú)論是否發(fā)生異常,都輸出“感謝使用本程序!”

package 異常;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestArray {
  public static void main(String[] args) {
    int a[] = new int[5];
    System.out.println("請(qǐng)輸入5個(gè)數(shù):");
    System.out.println("最后輸入一個(gè)非數(shù)字結(jié)束輸入操作。");
    Scanner in = new Scanner(System.in);
    try{
      int i = 0;
      while(in.hasNextDouble()){   
        a[i] = in.nextInt();
        i++;
      }
      if(i<5)
        throw new ArrayIndexOutOfBoundsException();
      for(int j=0;j<5;j++)
        System.out.print(a[j]+" ");
      System.out.println();
    }catch(InputMismatchException e1){
      System.err.println("請(qǐng)輸入整數(shù)作為數(shù)組元素!");
      e1.printStackTrace();
    }catch(ArrayIndexOutOfBoundsException e2){
      System.err.println("請(qǐng)輸入5個(gè)數(shù)!");
      e2.printStackTrace();
    }finally{
      System.out.print("感謝使用本程序!");
    }
  }
}

Java編程異常簡(jiǎn)單代碼示例

總結(jié)

以上就是本文關(guān)于Java編程異常簡(jiǎn)單代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Java異常退出條件的判斷示例代碼、Java編程異常處理最佳實(shí)踐【推薦】、Java編程中的檢查型異常與非檢查型異常分析等,有什么問(wèn)題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。感謝朋友們對(duì)本站的支持!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI