You Are Reading

0

Simple Addition, Division, and Do While in Java

BufferedReader , ,


Create a Program that will add or divide, then ask the user if they'd want to perform the computation again using Do While

Output:

Source Code:

import java.io.*;
public class AddDivide {
private static PrintStream p=System.out;
public static BufferedReader q=new BufferedReader(new InputStreamReader (System.in));
public static void main (String []args)throws IOException{

p.println("MAIN MENU");
p.println();
p.println("[A] Addition");
p.println("[D] Division");
p.println("[E] Exit");
p.println();



p.print("Input Operation: ");
String input=q.readLine();
String o;


if (input.equals("A")|| input.equals("a"))
{
do{
p.print("Enter First Number: ");
String num1=q.readLine();
double n1=Double.parseDouble(num1);

p.print("Enter Second Number: ");
String num2=q.readLine();
double n2=Double.parseDouble(num2);

double sum;
sum=n1+n2;

p.println("The SUM is: " +sum);

p.println("Perform Addition again? [Y/N] ");
o=q.readLine();
}
while(o.equals("Y") || o.equals("y"));
}
else if (input.equals("D") || input.equals("d"))
{
do{
p.print("Enter First Number: ");
String num1=q.readLine();
double n1=Double.parseDouble(num1);

p.print("Enter Second Number: ");
String num2=q.readLine();
double n2=Double.parseDouble(num2);

double quo;
quo=n1/n2;

p.println("The quotient is: " +quo);

p.println("Perform Division again? [Y/N] ");
o=q.readLine();
}
while(o.equals("Y") || o.equals("y"));
}
else if (input.equals("E") || input.equals("e"))
{
System.exit(0);
}
else
{
p.print("Invalid Input!");
}

}

}

0 comments:

Post a Comment

 
Copyright 2010 BufferedReader