import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SwitchCaseExample
{
public static void main(String args[]) throws IOException
{
int a,b,n;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter two numbers:");
a=Integer.valueOf(br.readLine()).intValue();
b=Integer.valueOf(br.readLine()).intValue();
System.out.println("Enter 1 for Add "+a+","+b);
System.out.println("Enter 2 for Subtract "+a+","+b);
System.out.println("Enter 3 for Multiply "+a+","+b);
System.out.println("Enter 4 for Divide "+a+","+b);
System.out.println("Enter Your choice?");
n=Integer.valueOf(br.readLine()).intValue();
switch (n)
{
case 1:
System.out.println(a+"+"+b+"="+(a+b));
break;
case 2:
System.out.println(a+"-"+b+"="+(a-b));
break;
case 3:
System.out.println(a+"*"+b+"="+(a*b));
break;
case 4:
System.out.println(a+"/"+b+"="+((double)a/b));
break;
default:
System.out.println("Invalid Entry.");
break;
}
}
catch (NumberFormatException ne)
{
System.out.println(ne.getMessage()+" is not a numeric value.Please try again with a numeric value.");
}
}
}
Output1 =>
Enter two numbers:
25
4
Enter 1 for Add 25,4
Enter 2 for Subtract 25,4
Enter 3 for Multiply 25,4
Enter 4 for Divide 25,4
Enter Your choice?
4
25/4=6.25
Output2 =>
Enter two numbers:
25
8
Enter 1 for Add 25,8
Enter 2 for Subtract 25,8
Enter 3 for Multiply 25,8
Enter 4 for Divide 25,8
Enter Your choice?
5
Invalid Entry.
Output3 => (Not given any choice.)
Enter two numbers:
25
6
Enter 1 for Add 25,6
Enter 2 for Subtract 25,6
Enter 3 for Multiply 25,6
Enter 4 for Divide 25,6
Enter Your choice?
For input string: "" is not a numeric value.Please try again with a numeric value.
import java.io.IOException;
import java.io.InputStreamReader;
public class SwitchCaseExample
{
public static void main(String args[]) throws IOException
{
int a,b,n;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter two numbers:");
a=Integer.valueOf(br.readLine()).intValue();
b=Integer.valueOf(br.readLine()).intValue();
System.out.println("Enter 1 for Add "+a+","+b);
System.out.println("Enter 2 for Subtract "+a+","+b);
System.out.println("Enter 3 for Multiply "+a+","+b);
System.out.println("Enter 4 for Divide "+a+","+b);
System.out.println("Enter Your choice?");
n=Integer.valueOf(br.readLine()).intValue();
switch (n)
{
case 1:
System.out.println(a+"+"+b+"="+(a+b));
break;
case 2:
System.out.println(a+"-"+b+"="+(a-b));
break;
case 3:
System.out.println(a+"*"+b+"="+(a*b));
break;
case 4:
System.out.println(a+"/"+b+"="+((double)a/b));
break;
default:
System.out.println("Invalid Entry.");
break;
}
}
catch (NumberFormatException ne)
{
System.out.println(ne.getMessage()+" is not a numeric value.Please try again with a numeric value.");
}
}
}
Output1 =>
Enter two numbers:
25
4
Enter 1 for Add 25,4
Enter 2 for Subtract 25,4
Enter 3 for Multiply 25,4
Enter 4 for Divide 25,4
Enter Your choice?
4
25/4=6.25
Output2 =>
Enter two numbers:
25
8
Enter 1 for Add 25,8
Enter 2 for Subtract 25,8
Enter 3 for Multiply 25,8
Enter 4 for Divide 25,8
Enter Your choice?
5
Invalid Entry.
Output3 => (Not given any choice.)
Enter two numbers:
25
6
Enter 1 for Add 25,6
Enter 2 for Subtract 25,6
Enter 3 for Multiply 25,6
Enter 4 for Divide 25,6
Enter Your choice?
For input string: "" is not a numeric value.Please try again with a numeric value.
No comments:
Post a Comment