ITeye博客

计算器

加菲学Java · 2010-09-20 16:34 · Java SE · 编程语言 · Markdown

下午作业做的一个小程序:

package work09_20;

import java.io.IOException;
import java.util.Scanner;

/*11*/
public class Test08 {
	public static void main(String[] args) throws IOException {
		js:
		while(true){
			Scanner scan = new Scanner(System.in);
			System.out.println("请选择操作     1-计算      2-退出:");
			String s = scan.next();
			int x = Integer.parseInt(s);
			switch (x) {
			case 1:
				scan = new Scanner(System.in);
				System.out.println("输入操作数1:");
				String s1 = scan.next();
				double m1 = Double.parseDouble(s1);

				System.out.println("输入运算符 + - * /:");
				String s2 = scan.next();
				char m2 = s2.charAt(0);

				System.out.println("输入操作数2:");
				String s3 = scan.next();
				double m3 = Double.parseDouble(s3);
				
				System.out.println("计算结果为:");
				System.out.print(m1 + "" + m2 + "" + m3 + "=");
				if (m2 == '+') {
					System.out.println(m1 + m3);
					continue js;
				} else if (m2 == '-') {
					System.out.println(m1 - m3);
					continue js;
				} else if (m2 == '*') {
					System.out.println(m1 * m3);
					continue js;
				} else if (m2 == '/') {
					System.out.println(m1 / m3);
					continue js;
				}
			case 2:
				System.exit(1);
			}
		}
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。