Wednesday, 21 August 2013

Taking Input in Java Programming



Taking Input From User In Java Programs


Hello, I am Aditya Chauhan...


Follow this blog to get information and share what you know about computer hardware, networking  ,programming, picture editing etc.


Here's a Java program to create a calculator. This is a menu driven program. It takes input of two integers and show the calculations as the option chosen.


//Program Starts Here
import java.io.*;
class calc
{
public static void main(String arg[]) throws IOException
{
int a,b,opt;
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
do
{
System.out.println(" Main Menu");
System.out.println(" 1.Add Two Numbers");
System.out.println(" 2.Subtract Two Numbers");
System.out.println(" 3.Multiply Two Numbers");
System.out.println(" 4.Divide Two Numbers");
System.out.println(" 5.Exit");
opt=Integer.parseInt(br.readLine());
switch(opt)
{
case 1:
System.out.println("Enter the first number: ");
a=Integer.parseInt(br.readLine());
System.out.println("Enter the second number: ");
b=Integer.parseInt(br.readLine());
System.out.println("Answer:  "+a+" + "+b+" = "+(a+b));
break;
case 2:
System.out.println("Enter the first number: ");
a=Integer.parseInt(br.readLine());
System.out.println("Enter the second number: ");
b=Integer.parseInt(br.readLine());
System.out.println("Answer:  "+a+" - "+b+" = "+(a-b));
break;
case 3:
System.out.println("Enter the first number: ");
a=Integer.parseInt(br.readLine());
System.out.println("Enter the second number: ");
b=Integer.parseInt(br.readLine());
System.out.println("Answer:  "+a+" * "+b+" = "+(a*b));
break;
case 4:
System.out.println("Enter the first number: ");
a=Integer.parseInt(br.readLine());
System.out.println("Enter the second number: ");
b=Integer.parseInt(br.readLine());
System.out.println("Answer:  "+a+" / "+b+" = "+(a/b));
break;
case 5:
break;
default:
System.out.println("Invalid Option!");
}
}while(opt!=5);
}
}
//Program Ends Here

Please Comment To Express Your Views

No comments:

Post a Comment