I'm new to JAVA and need some help writing a simple calculator program.
Here are the instructions:
-expects espression with 2 operands together with either +, -, *, or / operator
-espects only positive input
-assumes all real #'s
-assumes operands and operators in expression are separated with spaces
here is what I have so far:
import java.io.*;
import java.util.*;
public class Calculator
{
public static void main(String[] args)
throws java.io.IOExcep tion
{
String inputString, s1, s2, s3;
float num1, num2, num3, result;
InputStreamRead er isr = new InputStreamRead er(System.in);
BufferedReader br = new BufferedReader( isr);
System.out.prin t("What do you want to compute? ");
inputString = br.readLine();
StringTokenizer st = new StringTokenizer (inputString);
s1 = st.nextToken();
s2 = st.nextToken();
s3 = st.nextToken();
num1 = Float.parseFloa t(s1);
num2 = Float.parseFloa t(s3);
if (s2.equals("+") )
result = (num1 + num2);
else if (s2.equals("-"))
result = (num1 - num2);
else if (s2.equals("/"))
result = (num1 / num2);
else if (s2.equals("*") )
result = (num1 * num2);
System.out.prin tln("The result is " + result);
}
}
Here are the instructions:
-expects espression with 2 operands together with either +, -, *, or / operator
-espects only positive input
-assumes all real #'s
-assumes operands and operators in expression are separated with spaces
here is what I have so far:
import java.io.*;
import java.util.*;
public class Calculator
{
public static void main(String[] args)
throws java.io.IOExcep tion
{
String inputString, s1, s2, s3;
float num1, num2, num3, result;
InputStreamRead er isr = new InputStreamRead er(System.in);
BufferedReader br = new BufferedReader( isr);
System.out.prin t("What do you want to compute? ");
inputString = br.readLine();
StringTokenizer st = new StringTokenizer (inputString);
s1 = st.nextToken();
s2 = st.nextToken();
s3 = st.nextToken();
num1 = Float.parseFloa t(s1);
num2 = Float.parseFloa t(s3);
if (s2.equals("+") )
result = (num1 + num2);
else if (s2.equals("-"))
result = (num1 - num2);
else if (s2.equals("/"))
result = (num1 / num2);
else if (s2.equals("*") )
result = (num1 * num2);
System.out.prin tln("The result is " + result);
}
}
Comment