Page 127 - ComputerScience_Class_11
P. 127

Program 3     Write a program to input two integer numbers and print their sum.


                   1      import java.util.*;

                   2      class try_catch
                   3      {

                   4          public static void main(String[] args)

                   5          {
                   6              Scanner sc = new Scanner(System.in);

                   7              int a,b,s;

                   8              try
                   9              {

                   10                 System.out.println("Enter 2 numbers  : ");

                   11                 a=sc.nextInt();
                   12                 b=sc.nextInt();

                   13                 s=a+b;

                   14                 System.out.println("The result is : "+s);

                   15             }
                   16             catch(Exception ex)

                   17             {

                   18                 System.out.println("You are supposed to enter an integer value");
                   19             }

                   20         }

                   21     }

                 The output of the preceding program is as follows: (If taken a double value)

                         BlueJ: Terminal Window - Java

                     Options

                    Enter 2 numbers :
                    3
                    5.3
                    You are supposed to enter an integer value











                                                                                                          Objects  125
   122   123   124   125   126   127   128   129   130   131   132