求简单JAVA程序代码,能操作的,简单的计算器有计算功能+-*⼀就行越简单越好!!!急!!!

2025年05月06日 07:56
有1个网友回答
网友(1):

public static void main(String[] args) {
String st = "";
计算器 j = new 计算器();

boolean t = true;
while (t) {
System.out.print("请输入计算代码:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
st = br.readLine();
} catch (IOException ex) {
Logger.getLogger(计算器.class.getName()).log(Level.SEVERE, null, ex);
}
if (st.equals("end")) {
t = false;
} else if (!st.equals("")) {
j.处理(st);
}
}
}

public void 处理(String st) {
if (st.indexOf("+") != -1) {
加(符号处理(st, "+"));
} else if (st.indexOf("-") != -1) {
减(符号处理(st, "-"));
} else if (st.indexOf("*") != -1) {
乘(符号处理(st, "*"));
} else if (st.indexOf("/") != -1) {
除(符号处理(st, "/"));
}
}

public int[] 符号处理(String st, String s) {
int[] i = new int[2];
i[0] = Integer.parseInt(st.substring(0, st.indexOf(s)));
i[1] = Integer.parseInt(st.substring(st.indexOf(s) + 1));
return i;
}

public void 加(int x[]) {
System.out.println(x[0] + "+" + x[1] + "=" + (x[0] + x[1]));
}

public void 减(int[] x) {
System.out.println(x[0] + "-" + x[1] + "=" + (x[0] - x[1]));
}

public void 乘(int[] x) {
System.out.println(x[0] + "*" + x[1] + "=" + x[0] * x[1]);
}

public void 除(int x[]) {
if (x[1] == 0) {
System.out.print("除数不能为零");
return;
}
System.out.println(x[0] + "/" + x[1] + "=" + x[0] / x[1]);
}
可以运行:输入示例:55+62