计算器
package com.li.changGe.method;
import java.util.Scanner;
public class Calculator {
static double[] array = new double[2];
static Scanner scanner = new Scanner(System.in);
static double result = 0;
static boolean flag = true;
public static void main(String[] args) {
System.out.println("请输入一个数");
array[0] = scanner.nextDouble();
while(flag){
String resultInput = input();
result = traverse(resultInput);
System.out.println(result);
array[0] = result;
}
System.out.println("请输入正确的操作");
}
public static double traverse(String input){
switch (input){
case "+":
result = sum(array);
break;
case "-":
result = subtraction(array);
break;
case "*":
result = multiplication(array);
break;
case "/":
result = division(array);
break;
default:
flag = false;
}
return result;
}
public static String input(){
System.out.println("请输入第二个数.注意:被除数不可为0");
array[1] = scanner.nextDouble();
System.out.println("选择您要执行的操作:+,-,*,/.输入其他退出程序");
scanner.nextLine();
String letter = scanner.nextLine();
return letter;
}
public static double sum(double[] num){
return num[0] + num[1];
}
public static double subtraction(double[] num){
return num[0] - num[1];
}
public static double multiplication(double[] num){
return num[0] * num[1];
}
public static double division(double[] num){
if(num[1] != 0){
return num[0] / num[1];
}
System.out.println("被除数不可为0");
return num[0];
}
}