天天看点

Java stdin 示例

package com.ross.Calc;

import java.util.Scanner ;

public class Calculator {

    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);

        String str = "" ;

        Expression e = new Expression();

        while(true){

            System.out.println("Input an expresion or 'q' to quit");

            str = input.nextLine();

            if(str.equalsIgnoreCase("q"))

                break ;

            if(e.Parse(str)){

                Operation op = OperationFactory.Instance().CreateOperation(e.getOp());

                if(op == null) {

                    System.err.println("Non Operation " + e.getOp());

                    continue ;

                }

                op.setNumA(e.getNumA());

                op.setNumB(e.getNumB());

                try {

                    System.out.println("Result of "+op.getNumA() + op.GetOperator() + op.getNumB() +": " + op.GetResult());

                }

                catch(Exception ex) {

                    System.err.println("Error :" + ex.getMessage());

                }

            }

        }

    }

}