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 BT984

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.

Liên kết rút gọn:

https://gokisoft.com/984

Bình luận

avatar
Triệu Văn Lăng [T2008A]
2021-03-03 06:36:11



/*
 * 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 bai984_1;

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

/**
 *
 * @author MTLS
 */
public class main {

    public static void main(String[] args) throws InputMismatchException {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap x: ");
        float x = scan.nextFloat();

        System.out.println("Nhap y: ");
        float y = scan.nextFloat();
        float s;
        try {

            s = hamchia.chia(x, y);
            System.out.println("s: " + s);
        } catch (Exception ex) {
            ex.printStackTrace();

        } finally {
            System.out.println("Ket thuc");
        }
//        throw new Exception("Test throws");
    }
}


avatar
Triệu Văn Lăng [T2008A]
2021-03-03 06:35:48



/*
 * 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 bai984_1;

/**
 *
 * @author MTLS
 */
public class hamchia {
    public static float chia(float x, float y) throws Exception {
        if (y==0) {
            throw new Exception("Phep chia khong hop le, vui long nhap y khac 0"); 
        }
        return x/y;
    }
}


avatar
Nguyễn Tiến Đạt [T2008A]
2021-03-02 15:51:35


#Bai1.java


/*
 * 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 lesson8.Exception;

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

/**
 *
 * @author MyPC
 */
public class Bai1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        float x,y;
        try {
            System.out.println("Nhap x:");
            x = scan.nextInt();
            System.out.println("Nhap y:");
            y = scan.nextInt();
            float z = chia(x, y);
            System.out.println("Thuong: " + z);
        } catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("Error > InputMismatchException");
        } catch (ArithmeticException e){
            e.printStackTrace();
            System.out.println("Error > ArithmeticException");
        } finally{
            System.out.println("Ket thuc!!");
        }
    }
    
    static float chia(float x , float y){
        return x / y;
    }
}


#Bai2.java


/*
 * 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 lesson8.Exception;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author MyPC
 */
public class Bai2 {
    public static void main(String[] args) {
        input();
    }
    
    public static void input(){
        ArrayList<Integer> list = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        int a;
            try{
                while(true){
                    System.out.println("Nhap:");
                    a = scan.nextInt();
                    if(a==100){
                        throw new Exception("Ban da nhap so 100!!");
                    }
                    list.add(a);
                }
            }catch (Exception e) {
                  e.printStackTrace();
//                System.out.println("Ban da nhap so 100!!");
            }finally{
                for (Integer integer : list) {
                    System.out.print(integer + " ");
                }
                System.out.println("");
                System.out.println("Finish");
            }     
    }
}


avatar
Trần Văn Lâm [T2008A]
2021-03-01 13:01:39


bai1
/*
 * 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 b1;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Bai1{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap 2 so thuc a,b:");
        try{
            Float a,b;
            System.out.println("Nhap a =");
            a = scan.nextFloat();
            System.out.println("Nhap b =");
            b = scan.nextFloat();
        }catch(Exception e){
            System.out.println(e);
        }
    }
    public static float PhepChia(float a, float b) throws Exception {
        if (a == 0) {
            throw new Exception("Khong hop le!!!");
        } else {
            return a / b;
        }
    }
}


avatar
hainguyen [T2008A]
2021-03-01 10:21:17



Bai 1/*
 * 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 lesson10;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap x: ");
        float x = scan.nextInt();
        
        System.out.println("Nhap y: ");
        float y = scan.nextInt();
        
        try {
            float s = x/y;
            System.out.println("s: " + s);
        } catch(Exception ex) {
            System.out.println("Eroor...");
        } finally {          
            System.out.println("Xoa thanh cong.");
            
        }
        
    }
}


avatar
hoangkhiem [C1907L]
2020-04-01 10:53:40



/*
 * 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 Exception;

/**
 *
 * @author Admin
 */
public class MyException extends Exception {

    private int[] x;
    private int lastN;

    public MyException(int[] x, int lastN) {
        this.x = x;
        this.lastN = lastN;
    }

    public void printErr() {

        System.out.println("Mảng bạn vừa nhập là ");
        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 Exception;

import java.util.Scanner;

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

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

    public static void nhap(int[] mang, int n) {
        Scanner input = new Scanner(System.in);
        int i = 0;
        try {
            System.out.println("*****************************************************");
            System.out.println("Bạn không được phép nhập 100 vì sẽ làm chương trình tự in ra những gì bạn nhập");
            System.out.println("*****************************************************");
            for (; i < n; i++) {
                System.out.format("Nhap vao phan tu thu %d:", i + 1);
                mang[i] = Integer.parseInt(input.nextLine());
                if (mang[i] == 100) {
                    System.err.println("Bạn đã nhập 100 !!!!!");
                    throw new MyException(mang, i);
                }
            }
        } catch (MyException e) {
            e.printErr();
        }
    }
}


avatar
hoangkhiem [C1907L]
2020-03-31 16:56:16



/*
 * 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 battap303;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Exception {

    public static void main(String[] args) {
        Scanner nhap = new Scanner(System.in);
float a = 0f;
float b =0f;
        try {
            System.out.println("nhap a");
             a = Integer.parseInt(nhap.nextLine());
            System.out.println("nhap b");
             b = Integer.parseInt(nhap.nextLine());
             System.out.println(devide(a, b));
        } catch (NumberFormatException  ex) {
            System.out.println("Mời bạn nhập số nguyên");
        }catch(ArithmeticException e){
            System.out.println(e.getMessage());
        }
//    try{
//        System.out.println(devide(a,b));
//    }catch(ArithmeticException e){
//         System.out.println(e.getMessage());
//    }}
    }
    public static float devide(float a, float b) {
        if (b == 0) {
            throw new ArithmeticException("Cannot devide by 0");
        } else {
            System.out.println("kết quả");
            return a / b;
        }
    }
}


avatar
Hoàng Quang Huy [C1907L]
2020-03-31 08:52:16



package ExceptionTest;

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

public class Main {

    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        float x = 0f, y = 0f;
        boolean flag = true;
        try {
            System.out.println("Nhập số thực thứ 1: ");
            x = input.nextFloat();
            System.out.println("Nhập số thực thứ 2");
            y = input.nextFloat();

        } catch (InputMismatchException e) {
            System.out.println("Số vừa nhập không phải số thực");
            flag = false;
        }
        if (flag) {
            try {
                System.out.println("Result = " + div(x, y));
            } catch (ArithmeticException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public static float div(float x, float y) throws Exception {
        if (y == 0) {
            throw new ArithmeticException("Không chia cho 0");
        } else {
            return x / y;
        }
    }
}
-------------------------------------------------------------------------------------
package ExceptionTest;

import java.util.Scanner;

public class ExceptionArray {

    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhập số phần tử của mảng: ");
        int n = input.nextInt();
        int[] arr = new int[n];
        try {
            for (int i = 0; i < n; i++) {
                System.out.println("Nhập giá trị cho phần tử thứ " + i + 1);
                arr[i] = input.nextInt();
                if (arr[i] == 100) {
                    throw new Exception("Invalid value");
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            print(arr);
        }
    }

    public static void print(int[] a) {
        System.out.println("Giá trị phần tử trong mảng:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println("");
    }
}


avatar
Vũ Việt Đức [C1907L]
2020-03-30 16:19:46



/*
 * 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 lession7;

import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Bai1 {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Nhập vào 2 số thực: ");
        try{
            System.out.print("a = ");
            float a = Float.parseFloat(scanner.nextLine());
            System.out.print("b = ");
            float b = Float.parseFloat(scanner.nextLine());
            System.out.format("%f / %f = %f", a, b, chia(a,b));
        }catch(Exception e){
            System.out.println(e);
        }
    }
    
    public static float chia(float a, float b) throws Exception{
        if(a == 0 || b == 0){
            throw new Exception("Phép chia không hợp lệ!");
        }else{
            return a / b;
        }
    }
}



/*
 * 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 lession7;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Bai2 {
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        int n = 0;
        System.out.print("Nhập số phần tử cho mảng: ");
        n = Integer.parseInt(scanner.nextLine());
        List<Integer> newArr = new ArrayList<>();
        
        try{
            newArr = nhap(n);
        }catch(Exception e){
            System.out.println(e);
        }
    }
    public static List<Integer> nhap(int n) throws Exception{
        Scanner scanner = new Scanner(System.in);
        List<Integer> intArr = new ArrayList<>();
        int value;
        int count = 0;
        for(int i = 0; i < n; i++){
            System.out.print("Phần tử thứ " + i + ": ");
            value = Integer.parseInt(scanner.nextLine());
            if(value == 100){
                count++;
                break;
            }else{
                intArr.add(value);
            }
        }
        
        if(count > 0){
            for(int i = 0; i < intArr.size() ; i++){
                throw new Exception("Giá trị nhập vào là 100!\nChương trình kết thúc.\nDanh sách các phần tử đã nhập là:" + show(intArr));
            }
        }else{
            System.out.println("Danh sách các phần tử đã nhập là:" + show(intArr));
        }
        
        return intArr;
        
    }
    
    public static String show(List<Integer> arr){
        String str = "\n";
        for(int i = 0; i < arr.size(); i++){
            str = str + arr.get(i) + "\n";
        }
        return str;
    }
}


avatar
Nguyễn Hữu Đạt [C1907L]
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");
                }
		
		
		

}
}