1、输入三个参数,如100,+,200,则返回300,第1、3个参数只能输入数字,第2个参数输入+、-、*、/,返回运算结果。 请注意界面校验、异常处理等环节,请自行挑选winform、网页或者命令行形式编写
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class EnterConsole {
public static void main(String[] args) throws IOException {
computer();
}
public static void computer() throws IOException {
EnterConsole enterConsole = new EnterConsole();
int first = enterConsole.inputConsoleIn(null);
String fuhao = enterConsole.inputConsoleFuhao();
int second = enterConsole.inputConsoleIn(fuhao);
if("/".equals(fuhao) && second==0){
System.out.print("被除数不能为0,请重新输入被除数:");
}
if("+".equals(fuhao)){
System.out.println("结果是:"+(first+second));
}else if("-".equals(fuhao)){
System.out.println("结果是:"+(first-second));
}else if("*".equals(fuhao)){
System.out.println("结果是:"+(first*second));
}else if("/".equals(fuhao)){
System.out.println("结果是:"+(first/second));
}else{
System.out.println("NONE");
}
System.out.print("是否继续?y/n");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
if("y".equals(str)){
computer();
}
}
/**
* 输入数字
*/
public int inputConsoleIn(String fuhao) throws IOException {
System.out.print("请输入数字:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputStr = br.readLine();
try{
int num = (Integer.valueOf(inputStr));
if("/".equals(fuhao)){
System.out.print("被除数不能为0,请重新输入,请输入数字:");
return inputConsoleIn(fuhao);
}else{
return num;
}
}catch(Exception ex){
System.out.print("输入的不是数字,请重新输入,请输入数字:");
return inputConsoleIn(fuhao);
}
}
/**
* 输入+-*\/
* @return
* @throws IOException
*/
public String inputConsoleFuhao() throws IOException {
System.out.print("请输入计算符号(+-*/):");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputStr = br.readLine();
if("+".equals(inputStr) || "-".equals(inputStr) || "*".equals(inputStr) || "/".equals(inputStr)){
return inputStr;
}else{
return inputConsoleFuhao();
}
}
}