By GokiSoft.com| 20:05 03/02/2023|
Java Basic

Java Basic- Tổng hợp bài tập ôn luyện ngoại lên trong java - Exception in java

Bài 1:

Viết chương trình nhập vào 2 số thực. Bắt ngoại lệ để khi nhập vào không phải là số.

Cài đặt hàm chia, trong đó bắt ngoại lệ nếu số chia là 0 thì thông báo phép chia không hợp lệ và kết thúc chương trình.

 

Bài 2:

Khai báo 1 mảng có n phần tử các số nguyên, viết hàm nhập các phần tử cho mảng. Bắt ngoại lệ nếu nhập phần từ có giá trị là 100 thì in ra các phần tử đã nhập và kết thúc chương trình.

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Nguyễn Hữu Đạt [C1907L]
Nguyễn Hữu Đạt

2020-03-30 14:54:04



import exception.khaibao.InputMismatchException;
import java.util.Scanner;

public class test2 {
    public static void main (String[ ] args) throws InputMismatchException {
		Scanner sc = new Scanner(System.in);
		int n = 0;
                try{
                        System.out.print("Nhập số phẩn tử của mảng: " );
                        n = sc.nextInt();
                        int array[] = new int[n];
                        
                        for (int i = 0; i < n; i++) {
                            System.out.print("Nhập phần tử thứ " + i + ": ");
                            array[i] = sc.nextInt();
                            if(array[i] == 100){
                                break;
                            }
                        }
                        
                        System.out.println("\nCác phần tử bạn nhập là: ");
                        for (int i = 0; i < n; i++) {
                            System.out.print(array[i] + "\t");
                        }
                       
                }catch (Exception ex){
                        System.out.print("error");
                }
		
		
		

}

	
    
         
    
         
    // Hiển thị mảng vừa nhập
    
         
    

}



import exception.khaibao.InputMismatchException;
import java.util.Scanner;

public class test {

	public static void main (String[ ] args) throws InputMismatchException {
		Scanner sc = new Scanner(System.in);
		int a = 0;
		int b;
		int c;
                try{
                        System.out.print("Nhap a: " );
                        a = sc.nextInt();
                        System.out.print("Nhap b: " );
                        b = sc.nextInt();
                        c = a / b;
                        System.out.print("Thương c: " + c);
                }catch (Exception ex){
                        System.out.print("error");
                }
		
		
		

}
}



Nguyễn Hữu Đạt [C1907L]
Nguyễn Hữu Đạt

2020-03-30 14:53:31



import exception.khaibao.InputMismatchException;
import java.util.Scanner;

public class test2 {
    public static void main (String[ ] args) throws InputMismatchException {
		Scanner sc = new Scanner(System.in);
		int n = 0;
                try{
                        System.out.print("Nhập số phẩn tử của mảng: " );
                        n = sc.nextInt();
                        int array[] = new int[n];
                        
                        for (int i = 0; i < n; i++) {
                            System.out.print("Nhập phần tử thứ " + i + ": ");
                            array[i] = sc.nextInt();
                            if(array[i] == 100){
                                break;
                            }
                        }
                        
                        System.out.println("\nCác phần tử bạn nhập là: ");
                        for (int i = 0; i < n; i++) {
                            System.out.print(array[i] + "\t");
                        }
                       
                }catch (Exception ex){
                        System.out.print("error");
                }
		
		
		

}

	
    
         
    
         
    // Hiển thị mảng vừa nhập
    
         
    

}



import exception.khaibao.InputMismatchException;
import java.util.Scanner;

public class test {

	public static void main (String[ ] args) throws InputMismatchException {
		Scanner sc = new Scanner(System.in);
		int a = 0;
		int b;
		int c;
                try{
                        System.out.print("Nhap a: " );
                        a = sc.nextInt();
                        System.out.print("Nhap b: " );
                        b = sc.nextInt();
                        c = a / b;
                        System.out.print("Thương c: " + c);
                }catch (Exception ex){
                        System.out.print("error");
                }
		
		
		

}
}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-03-30 14:29:14



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package march30.Exception;

import java.util.*;

/**
 *
 * @author Redmibook 14
 */
public class Input2RealNumber {

    public static void devide(float a, float b) throws StringException {
        if (a == 0 || b == 0) {
            throw new StringException("Devide by Zero", a, b);
        } else {
            System.out.println("a / b = " + a / b);
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("input a = ");
        String Stra = scan.nextLine();
        System.out.println("input b = ");
        String Strb = scan.nextLine();
        try {
            int a = Integer.parseInt(Stra);
            int b = Integer.parseInt(Strb);
            devide(a, b);
        } catch (NumberFormatException e) {
            System.out.println("Please input 2 number");
        } catch (StringException e) {
            System.out.println(e.getMessage());
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package march30.Exception;

import java.util.*;

/**
 *
 * @author Redmibook 14
 */

public class StringException extends Exception {

    float x, y;

    public StringException(String message, float x, float y) {
        super(message);
        this.x = x;
        this.y = y;
    }

    @Override
    public String getMessage() {
        return "x / y = " + x + "," + y + ", " + super.getMessage();
    }
}



trung [C1907L]
trung

2020-03-30 13:52:03


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

import java.util.Scanner;


/**
 *
 * @author prdox
 */
public class Main {
//  Viết chương trình nhập vào 2 số thực. Bắt ngoại lệ để khi nhập vào không phải là số.
//  Cài đặt hàm chia, trong đó bắt ngoại lệ nếu số chia là 0 thì thông báo phép chia không hợp lệ và kết thúc chương trình.
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        float x = 0f,y = 0f;
        try {
            System.out.println("Nhap vao so thuc thu nhat:");
            x = Float.parseFloat(input.nextLine());
            System.out.println("Nhap vao so thuc thu 2:");
            y = Float.parseFloat(input.nextLine());
        } catch(NumberFormatException e) {
            System.out.println("So nhap vao khong phai la so thuc");
        }
        try {
            System.out.println(devide(x, y));
        } catch(ArithmeticException e) {
            System.out.println(e.getMessage());
        }
    }
    
    public static float devide(float x, float y){
        if (y==0){
            throw new ArithmeticException("Cannot devide by 0");
        } else{
            return x/y;
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

/**
 *
 * @author prdox
 */
public class MyException extends Exception{
    private int[] x;
    private int lastN;
    public MyException(String s,int [] x, int lastN) 
    { 
        // Call constructor of parent Exception 
        super(s);
        this.x = x;
        this.lastN = lastN;
    }
    public void printErr(){
        for (int i = 0; i < lastN; i++) {
            System.out.println(x[i]);
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Main {

    public static void main(String[] args) {
        int n;
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao n");
        n = input.nextInt();
        int[] arr = new int[n];
        get_input(arr, n);
    }

    public static void get_input(int[] arr, int n) {
        Scanner input = new Scanner(System.in);
        int i = 0;
        try {
            for (; i < n; i++) {
                System.out.format("Nhap vao phan tu thu %d:", i + 1);
                arr[i] = Integer.parseInt(input.nextLine());
                if (arr[i] == 100) {
                    throw new MyException("Not ok", arr, i);
                }
            }
        }catch (MyException e){
            e.printErr();
        }
    }
}



Ngô Quang Huy [C1907L]
Ngô Quang Huy

2020-03-30 13:51:10



package March30;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Bai1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = 0,y = 0,dk=1;
        try{
            System.out.print("Nhap vao x = ");
            x=input.nextInt();
            System.out.print("Nhap vao y = ");
            y=input.nextInt();
        }catch(InputMismatchException e){
            dk=0;
            System.err.println("Ban chi co the nhap vao so");
        }
        if(dk==1)  divide(x,y);
    }
    
    static void divide(int x, int y){
        float result;
        try{
            result = x/y;
            System.out.println("Ket qua la "+ result);
        }catch(ArithmeticException e){
            System.err.println("Ban khong the chia cho 0");
        }
    }
}



package March30;

import java.util.Scanner;

public class Bai2 {
    public static void main(String[] args) {
        int n;
        Scanner input = new Scanner(System.in);
        System.out.print("Nhap vao n: ");
        n = input.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++){
            try{
                arr[i] = input.nextInt();
                if(arr[i]>=100){
                    throw new Exception();
                }
            }catch( Exception e ){
                System.out.print("\nMang vua nhap la: ");
                for(int j=0;j<i;j++){
                    System.out.print(arr[j]+" ");
                }
                break;
            }
        }
    }
}