首页 > 代码库 > 控制台输入一个简单表达式(+-*/)计算结果

控制台输入一个简单表达式(+-*/)计算结果

1, 

import java.util.Scanner

/**
 * Author: bruce
 * Date: 2015-01-24
 */
object ConsoleScanner {

	def main(args: Array[String]) {

		println("Please enter a phase: ");
		val scan = new Scanner(System.in);
		val phase = scan.next()

		algo(phase, "+")
		algo(phase, "-")
		algo(phase, "*")
		algo(phase, "/")


	}

	def algo(phase: String, fu: String): Unit = {
		val arr = phase.split("[" + fu + "]")

		if (arr.length == 2) {

			try {
				arr(0).toInt
				arr(1).toInt

				println("result: " + (arr(0).toInt + arr(1).toInt))

			} catch {
				case e: NumberFormatException => println("type is not match")
			}

		}
	}

}



控制台输入一个简单表达式(+-*/)计算结果