Java Static Initializer Block

Sort by

recency

|

1420 Discussions

|

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int B = sc.nextInt();
        int H = sc.nextInt();
        try
        {
            if(B < 0 || B >= 100 && H < 0 || H >= 100)
            {
               throw(new Exception("java.lang.Exception: Breadth and height must be positive")); 
            }
            int area = B * H;
            System.out.println(area);   
    
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    

    }

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution { public static void main(String[] args) { Scanner s=new Scanner(System.in); int B=s.nextInt(); int H=s.nextInt(); if(B>0&&H>0) System.out.println(B*H); else System.out.println("java.lang.Exception: Breadth and height must be positive"); } }

  • + 0 comments

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = 0;
        boolean flag = true;
        if(a>=0 && b>=0){
            if(a>0 || a<0 && b>0 || b<0){
            c=a*b;
            if(c!=0 && c>=0){
                flag = false;
            }
            System.out.println(c);
        }
        }
    
        if(flag){
            try{
                throw(new IOException("java.lang.Exception: Breadth and height must be positive"));
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
        sc.close();
    }
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
        static int B;
        static int H;
        static boolean flag;
        static{
            Scanner sc = new Scanner(System.in);
            B=sc.nextInt();
            H=sc.nextInt();
            flag = true;
            try{
                if(B<=0 || H<=0){
                   flag = false;
                   throw(new Exception("java.lang.Exception: Breadth and height must be positive"));
                }
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            if(flag){
                System.out.print(B*H);
            }
    
        }
    }
    
  • + 0 comments

    public class Solution { static int B,H; static boolean flag =true; static{ Scanner sc = new Scanner(System.in); B=sc.nextInt(); H=sc.nextInt(); if(B<=0 || H<=0){ flag=false; System.out.println("java.lang.Exception: Breadth and height must be positive"); } }

    public static void main(String[] args){ if(flag){ int area=B*H; System.out.print(area); }

    }//end of main
    

    }//end of class