basically,i want to make a simple calculator
I want to take a string input from the keyboard, so if its a number or '.' it will add it to the String and if it is a char(+-*/) it will do the calculation.
So you first enter a 3
then a 4, which will make the string 34, it then converts it to a double then do a + then enter 3 then . then 34 so the string is 3.34 then you press = and it gives the answer. I have read in the value then
string newstring = value;
if (value.equals("0") |value.equals("1") etc
in = in + newstring;
}
then
开发者_如何转开发try
{
setOperand(convert in to double)
if (flag == 1)
{
operand1 = convert in to double
result = operand1
flag = 0;
}// only does it for the first operand
getresult() which is if + then result + getOperand
catch
try
changes to char
for some reason when i just do 3= the result = 0, or 3 then . then 2 etc
Can I make a suggestion? Get the entire String to calculate in one line, and then parse that line.
import java.util.Scanner;
public class AshanCalc {
public static void main(String[] args) {
System.out.println("Welcome to my calculator. Please enter a math equation:");
Scanner in = new Scanner(System.in);
String equation = in.nextLine(); // get the ENTIRE line from the keyboard
// not just one character at a time
double result = evaluateEquation(equation);
}
private static double evaluateEquation(String equation) {
/**
* your mission - fill this in
*/
}
}
Also use the method isDigit() of Char instead of using if(value.equals("0)||value.equals("1") etc .
精彩评论