KiKi实现一个简单计算器,实现两个数的“加减乘除”运算,用户从键盘输入算式“操作数1运算符操作数2”,计算并输出表达式的值,如果输入的运算符号不包括在(+、-、*、/)范围内,输出“Invalid operation!”。当运算符为除法运算,即“/”时。如果操作数2等于0.0,则输出“Wrong!Division by zero!”
数据范围:字符串长度满足 ,保证运算符是一个char类型字符。
输入一行字符串,操作数1+运算符+操作数2 (其中合法的运算符包括:+、-、*、/)。
输出为一行。
如果操作数和运算符号均合法,则输出一个表达式,操作数1运算符操作数2=运算结果,各数小数点后均保留4位,数和符号之间没有空格。
如果输入的运算符号不包括在(+、-、*、/)范围内,输出“Invalid operation!”。当运算符为除法运算,即“/”时。
如果操作数2等于0.0,则输出“Wrong!Division by zero!”。
1.0+3.0
1.0000+3.0000=4.0000
1.0;4.0
Invalid operation!
44.0/0.0
Wrong!Division by zero!
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String inp = in.next(); Double a; Double b; if (inp.contains("+")) { a = Double.parseDouble(inp.substring(0, inp.indexOf("+"))); b = Double.parseDouble(inp.substring(inp.indexOf("+")+1)); System.out.printf("%.4f+%.4f=%.4f", a, b, a + b); } else if (inp.contains("-")) { a = Double.parseDouble(inp.substring(0, inp.indexOf("-"))); b = Double.parseDouble(inp.substring(inp.indexOf("-")+1)); System.out.printf("%.4f-%.4f=%.4f", a, b, a - b); } else if (inp.contains("*")) { a = Double.parseDouble(inp.substring(0, inp.indexOf("*"))); b = Double.parseDouble(inp.substring(inp.indexOf("*")+1)); System.out.printf("%.4f*%.4f=%.4f", a, b, a * b); } else if (inp.contains("/")) { a = Double.parseDouble(inp.substring(0, inp.indexOf("/"))); b = Double.parseDouble(inp.substring(inp.indexOf("/")+1)); if (b == 0.0) { System.out.printf("Wrong!Division by zero!"); } else { System.out.printf("%.4f/%.4f=%.4f", a, b, a / b); } } else { System.out.printf("Invalid operation!"); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); int f = 0; for (int i = 0; i < s.length(); i++) { char cha = s.charAt(i); if (cha == '+' || cha == '-' || cha == '*' || cha == '/') { f = i; break; } } if (f == 0) { System.out.println("Invalid operation!"); } else { Double a = Double.valueOf(s.substring(0, f)); String b = s.substring(f, f + 1); Double c = Double.valueOf(s.substring(f + 1)); Double d = 0.0; if (b.equals("+")) { d = a + c; System.out.printf("%.4f+%.4f=%.4f", a, c, d); } else if (b.equals("-")) { d = a - c; System.out.printf("%.4f-%.4f=%.4f", a, c, d); } else if (b.equals("*")) { d = a * c; System.out.printf("%.4f*%.4f=%.4f", a, c, d); } else if (b.equals("/")) { if (c == 0) { System.out.printf("Wrong!Division by zero!"); } else { d = a / c; System.out.printf("%.4f/%.4f=%.4f", a, c, d); } } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ String str = scanner.nextLine(); //将运算符号与数字分隔开 String[] toStr = str.split("\\+|\\*|/|-|}| "); //将字符串类型转成double型 Double[] num = new Double[toStr.length]; for(int i = 0;i < toStr.length;i++){ num[i] = new Double(toStr[i]); } //运算 if(str.contains("+")){ System.out.printf(String.format("%.4f",num[0])); System.out.printf("+"); System.out.printf(String.format("%.4f",num[1])); System.out.printf("="); System.out.printf(String.format("%.4f",(num[0] + num[1]))); }else if(str.contains("-")){ System.out.printf(String.format("%.4f",num[0])); System.out.printf("-"); System.out.printf(String.format("%.4f",num[1])); System.out.printf("="); System.out.printf(String.format("%.4f",(num[0] - num[1]))); }else if(str.contains("*")){ System.out.printf(String.format("%.4f",num[0])); System.out.printf("*"); System.out.printf(String.format("%.4f",num[1])); System.out.printf("="); System.out.printf(String.format("%.4f",(num[0] * num[1]))); }else if(str.contains("/")){ if(num[1] == 0.0){ System.out.printf("Wrong!Division by zero!"); }else{ System.out.printf(String.format("%.4f",num[0])); System.out.printf("/"); System.out.printf(String.format("%.4f",num[1])); System.out.printf("="); System.out.printf(String.format("%.4f",(num[0] / num[1]))); } }else{ System.out.printf("Invalid operation!"); } } } }
import java.util.*; public class Main{ public static void main(String args[]){ Scanner input=new Scanner(System.in); String s=input.nextLine(); String arr[]=s.split("\\+|\\*|/|-|}| ");//正则表达式,以+-*/分割传入参数 Double [] math=new Double[arr.length];//字符串类型数组转double for(int i=0;i<arr.length;i++){math[i]=new Double(arr[i]);} if(s.contains("+")) {//判断传入值如果含+,则进行加法,并保留4位小数 System.out.printf("%.4f",math[0]); System.out.printf("+"); System.out.printf("%.4f",math[1]); System.out.printf("="); System.out.printf("%.4f",math[0]+math[1]); } else if(s.contains("-")) { System.out.printf("%.4f",math[0]); System.out.printf("-"); System.out.printf("%.4f",math[1]); System.out.printf("="); System.out.printf("%.4f",math[0]-math[1]); } else if(s.contains("*")) { System.out.printf("%.4f",math[0]); System.out.printf("*"); System.out.printf("%.4f",math[1]); System.out.printf("="); System.out.printf("%.4f",math[0]*math[1]); } else if(s.contains("/")) { if(math[1]==0){ System.out.print("Wrong!Division by zero!"); } else{ System.out.printf("%.4f",math[0]); System.out.printf("/"); System.out.printf("%.4f",math[1]); System.out.printf("="); System.out.printf("%.4f",math[0]/math[1]); } } else{ System.out.print("Invalid operation!"); } input.close(); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String exp = scanner.nextLine(); String op = "+-*/"; int i; char ch = '0'; for (i = 0; i < exp.length(); i++) { ch = exp.charAt(i); if (ch != '.' && (ch < '0' || ch > '9')) { break; } } if (op.contains(ch + "")) { double num1 = Double.parseDouble(exp.substring(0, i)); double num2 = Double.parseDouble(exp.substring(i + 1)); double result; if (ch == '/' && num2 == 0.0) { System.out.println("Wrong!Division by zero!"); } else { switch (ch) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; default: result=0; } System.out.printf("%.4f%c%.4f=%.4f", num1, ch, num2,result); } } else { System.out.println("Invalid operation!"); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNextLine()) { //先用contains判判断算术操作符是否存在 //若不存在 打印错误信息并检测下一行 //若存在 用split方法分开 //用parseDouble把字符串转为数值 利用相应的操作符进行运算 并打印结果 String str = scan.nextLine(); if (str.contains("+")) { String[] strs = str.split("\\+"); double a = Double.parseDouble(strs[0]); double b = Double.parseDouble(strs[1]); System.out.printf("%.4f+%.4f=%.4f", a, b, a + b); } else if (str.contains("-")) { String[] strs = str.split("-"); double a = Double.parseDouble(strs[0]); double b = Double.parseDouble(strs[1]); System.out.printf("%.4f-%.4f=%.4f", a, b, a - b); } else if (str.contains("*")) { String[] strs = str.split("\\*"); double a = Double.parseDouble(strs[0]); double b = Double.parseDouble(strs[1]); System.out.printf("%.4f*%.4f=%.4f", a, b, a * b); } else if (str.contains("/")) { String[] strs = str.split("/"); double a = Double.parseDouble(strs[0]); double b = Double.parseDouble(strs[1]); if (b == 0) { System.out.print("Wrong!Division by zero!"); } else { System.out.printf("%.4f/%.4f=%.4f", a, b, a / b); } } else { System.out.print("Invalid operation!"); } System.out.println(); } } }
import java.util.Date; import java.util.Iterator; import java.util.Scanner; import java.util.ServiceConfigurationError; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.contains("+")) { String[] s1 = s.split("\\+"); double a = Double.parseDouble(s1[0]); double b = Double.parseDouble(s1[1]); System.out.printf("%.4f+%.4f=%.4f\n", a, b, (a + b)); } else if (s.contains("-")) { String[] s1 = s.split("-"); double a = Double.parseDouble(s1[0]); double b = Double.parseDouble(s1[1]); System.out.printf("%.4f-%.4f=%.4f\n", a, b, (a - b)); } else if (s.contains("*")) { String[] s1 = s.split("\\*"); double a = Double.parseDouble(s1[0]); double b = Double.parseDouble(s1[1]); System.out.printf("%.4f*%.4f=%.4f\n", a, b, (a * b)); } else if (s.contains("/")) { String[] s1 = s.split("/"); double a = Double.parseDouble(s1[0]); double b = Double.parseDouble(s1[1]); if(b==0.0){ System.out.println("Wrong!Division by zero!"); } else {System.out.printf("%.4f/%.4f=%.4f\n", a, b, (a / b));} } else { System.out.println("Invalid operation!"); } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String string; String[] number; String operator; double result; boolean isNotOperation; while (scanner.hasNext()) { //考虑到会出现空格的情况,必须用nextLine()才能接收完整的数据 //next()在遇到空格时就会停止接收数据 string = scanner.nextLine(); //split()中的[]为正则表达式 number = string.split("[+\\-*/]"); //先判断是否为正确的运算符 //若不是,则number数组中只有源字符串(不会被分开),下边取运算符时会出现数组越界异常 if (number.length < 2) { System.out.println("Invalid operation!"); } else { operator = string.substring(number[0].length(), number[0].length() + 1); result = 0; isNotOperation = false; if (Double.parseDouble(number[1]) == 0) { System.out.println("Wrong!Division by zero!"); } else { switch (operator) { case "+": result = Double.parseDouble(number[0]) + Double.parseDouble(number[1]); break; case "-": result = Double.parseDouble(number[0]) - Double.parseDouble(number[1]); break; case "*": result = Double.parseDouble(number[0]) * Double.parseDouble(number[1]); break; case "/": result = Double.parseDouble(number[0]) / Double.parseDouble(number[1]); break; } System.out.printf("%.4f%s%.4f=%.4f", Double.parseDouble(number[0]), operator, Double.parseDouble(number[1]), result); } } } } }