开发者

adding a string to another string turning to a double or char then calculating result

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 .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜