2016-01-12 4 views
2

Как заменить четыре метода на один, который будет обновлять переменную «str», чтобы соответствовать операторам «+ -/*» с «ADD, SUB, DIV или MULT»? Когда он проходит через инструкции case, я пытаюсь выяснить, как заставить оператор case распознать оператор, который был выбран через вход сканера, и сопоставить его с соответствующей строкой дескриптора.Объединение четырех методов в один

import java.util.Scanner; 

    public class Testor4 { 
    public static void main(String[] args) { 
    String s1 = getInput("Enter a number: "); 
    String s2 = getInput("Enter second number"); 
    String op = getInput("Enter operator: + -/* "); 
    double result = 0; 
    String str = " You chose to"; 
    try{ 
     switch(op){ 
     case "+": str += getOpNameAdd(str); result = getSum(s1,s2); break; 
     case "-": str += getOpNameSub(str); result = getSub(s1,s2); break; 
     case "/": str += getOpNameDiv(str); result = getDiv(s1,s2); break; 
     case "*": str += getOpNameMult(str); result = getMult(s1,s2); break; 
     default: System.out.println("not an operator."); return; 
     } 
    }catch(Exception e){ 
     System.out.println(e.getMessage()); 
    } 
    System.out.printf("%s%s%.2f","Result is: ",str,result); 
    } 
    private static double getSum(String s1, String s2){ 
    double d1 = Double.parseDouble(s1); 
    double d2 = Double.parseDouble(s2); 
    return d1 + d2; 
    } 
    private static double getSub(String s1, String s2){ 
    double d1 = Double.parseDouble(s1); 
    double d2 = Double.parseDouble(s2); 
    return d1 - d2; 
    } 
    private static double getDiv(String s1, String s2){ 
    double d1 = Double.parseDouble(s1); 
    double d2 = Double.parseDouble(s2); 
    return d1/d2; 
    } 
    private static double getMult(String s1, String s2){ 
    double d1 = Double.parseDouble(s1); 
    double d2 = Double.parseDouble(s2); 
    return d1 * d2; 
    } 
    public static String getOpNameAdd(String str){ 
    return str = " ADD!"; 
    } 
    public static String getOpNameSub(String str){ 
    return str = " Subtract!"; 
    } 
    public static String getOpNameDiv(String str){ 
    return str = " Divide!"; 
    } 
    public static String getOpNameMult(String str){ 
    return str = " Multiply!"; 
    } 
    public static String getInput(String prompt){ 
    System.out.println(prompt); 
    Scanner sc = new Scanner(System.in); 
    return sc.nextLine(); 
    } 
    } 
+0

Что вы видите, когда вы запускали свой код через отладчик? – shoover

+0

ЧТО? он отлично работает. Это не проблема, требующая отладки. Вы пытались запустить его, и это не удалось? Вопрос в том, как объединить четыре метода внизу, которые делают одну конкретную вещь, и возвращать слова «ADD

+0

Нет, я этого не делал, но вы сказали: «Я пытаюсь понять, как заставить аргумент case распознать оператора», из-за чего он звучит так, будто у вас проблемы с этой частью. – shoover

ответ

1
public class Testor4 { 
public static void main(String[] args) { 
    String s1 = getInput("Enter a number: "); 
    String s2 = getInput("Enter second number"); 
    String op = getInput("Enter operator: + -/* "); 
    double result = 0; 
    String str = " You chose to"; 
    try { 
     switch (op) { 
     case "+": 
      str += getOpName(op); 
      result = getSum(s1, s2); 
      break; 
     case "-": 
      str += getOpName(op); 
      result = getSub(s1, s2); 
      break; 
     case "/": 
      str += getOpName(op); 
      result = getDiv(s1, s2); 
      break; 
     case "*": 
      str += getOpName(op); 
      result = getMult(s1, s2); 
      break; 
     default: 
      System.out.println("not an operator."); 
      return; 
     } 
    } catch (Exception e) { 
     System.out.println(e.getMessage()); 
    } 
    System.out.printf("%s%s%.2f", "Result is: ", str, result); 
} 

private static double getSum(String s1, String s2) { 
    double d1 = Double.parseDouble(s1); 
    double d2 = Double.parseDouble(s2); 
    return d1 + d2; 
} 

private static double getSub(String s1, String s2) { 
    double d1 = Double.parseDouble(s1); 
    double d2 = Double.parseDouble(s2); 
    return d1 - d2; 
} 

private static double getDiv(String s1, String s2) { 
    double d1 = Double.parseDouble(s1); 
    double d2 = Double.parseDouble(s2); 
    return d1/d2; 
} 

private static double getMult(String s1, String s2) { 
    double d1 = Double.parseDouble(s1); 
    double d2 = Double.parseDouble(s2); 
    return d1 * d2; 
} 

public static String getOpName(String op) { 
    String opName = "not an operator."; 
    switch (op) { 
    case "+": 
     opName = " ADD!"; 
     break; 
    case "-": 
     opName = " Subtract!"; 
     break; 
    case "/": 
     opName = " Divide!"; 
     break; 
    case "*": 
     opName = " Multiply!"; 
     break; 
    } 
    return opName; 
} 

public static String getInput(String prompt) { 
    System.out.println(prompt); 
    Scanner sc = new Scanner(System.in); 
    return sc.nextLine(); 
} 

}

3

Почему бы не сделать это?

try{ 
    switch(op){ 
    case "+": str += " ADD!"; result = getSum(s1,s2); break; 
    case "-": str += " Subtract!"; result = getSub(s1,s2); break; 
    case "/": str += " Divide!"; result = getDiv(s1,s2); break; 
    case "*": str += " Multiply!"; result = getMult(s1,s2); break; 
    default: System.out.println("not an operator."); return; 
    } 
}catch(Exception e){ 
    System.out.println(e.getMessage()); 
} 

Если строки будут использоваться повторно в другом месте, вы можете также сделать это строковая константа:

public static final String OpNameAdd = " ADD!"; 
0

С одним способом, и более простым, вы можете прочитать одну введите и обработайте строку «префикс + суффикс», где + обозначает все возможные операции, с чем-то вроде static int indexOfAny(String str, char[] searchChars) получить подстроки prefi x, суффикс, затем переключитесь (op) в соответствии с оператором.

1

Я бы начал с написания enum (например, Operation) для инкапсуляции поведения, имени и символов. Что-то вроде,

enum Operation { 
    ADD("+", "Addition"), SUBTRACT("-", "Subtraction"), // 
    MULTIPLY("*", "Multiplication"), DIVIDE("/", "Division"); 
    String operSymbol; 
    String operName; 

    Operation(String operSymbol, String operName) { 
     this.operSymbol = operSymbol; 
     this.operName = operName; 
    } 

    String getName() { 
     return operName; 
    } 

    String getSymbol() { 
     return operSymbol; 
    } 

    public static Operation fromString(String str) { 
     if (str != null) { 
      str = str.trim(); 
      if (!str.isEmpty()) { 
       for (Operation o : Operation.values()) { 
        if (str.equals(o.getSymbol())) { 
         return o; 
        } 
       } 
      } 
     } 
     return null; 
    } 

    public double performOperation(String s1, String s2) { 
     Double d1 = Double.parseDouble(s1); 
     Double d2 = Double.parseDouble(s2); 
     switch (this) { 
     case SUBTRACT: 
      return d1 - d2; 
     case MULTIPLY: 
      return d1 * d2; 
     case DIVIDE: 
      return d1/d2; 
     case ADD: 
     default: 
      return d1 + d2; 
     } 
    } 
} 

Пожалуйста, не открывайте новый сканер для каждого приглашения. Я передам его в метод. Мол,

public static String getInput(Scanner sc, String prompt) { 
    System.out.println(prompt); 
    return sc.nextLine(); 
} 

Тогда ваш метод main очень прост, вы получите нужный вход и вызывать методы по принципу Operation как

public static void main(String[] args) { 
    Scanner sc = new Scanner(System.in); 
    String s1 = getInput(sc, "Enter a number: "); 
    String s2 = getInput(sc, "Enter second number"); 
    String op = getInput(sc, "Enter operator: + -/* "); 
    try { 
     Operation oper = Operation.fromString(op); 
     if (op != null) { 
      double result = oper.performOperation(s1, s2); 
      System.out.printf("%s %s %s = %.2f (%s)%n", s1, // 
        oper.getSymbol(), s2, result, oper.getName()); 
     } else { 
      System.out.println("not an operator."); 
      return; 
     } 
    } catch (Exception e) { 
     System.out.println(e.getMessage()); 
    } 
} 
1

Вот как я бы это сделать. Начнем с интерфейса:

package cruft.arithmetic; 

/** 
* BinaryOperation is the interface for binary arithmetic operations +, -, *,/
* Created by Michael 
* Creation date 1/11/2016. 
* @link https://stackoverflow.com/questions/34734228/combining-four-methods-into-one 
*/ 
public interface BinaryOperation<T> { 

    T execute(T argument1, T argument2); 
} 

Реализация для добавления:

package cruft.arithmetic; 

/** 
* Addition implementation for BinaryOperation 
* Created by Michael 
* Creation date 1/11/2016. 
* @link https://stackoverflow.com/questions/34734228/combining-four-methods-into-one 
*/ 
public class AddOperation implements BinaryOperation<Double> { 

    @Override 
    public Double execute(Double argument1, Double argument2) { 
     return argument1 + argument2; 
    } 
} 

Вот Tester:

package cruft.arithmetic; 

/** 
* I think the class name is misspelled: "Tester". 
* Created by Michael 
* Creation date 1/11/2016. 
* @link https://stackoverflow.com/questions/34734228/combining-four-methods-into-one 
*/ 

import java.util.HashMap; 
import java.util.Map; 
import java.util.Scanner; 

public class Tester { 

    private static final Map<String, BinaryOperation<Double>> OPERATIONS = new HashMap<String, BinaryOperation<Double>>() {{ 
     put("+", new AddOperation()); 
    }}; 
    private static Scanner sc = new Scanner(System.in); 


    public static void main(String[] args) { 
     BinaryOperation<Double> operator = null; 
     do { 
      try { 
       String arg1 = getInput("1st argument : "); 
       String arg2 = getInput("2nd argument : "); 
       String oper = getInput("operator + - * /: "); 
       operator = OPERATIONS.get(oper); 
       if (operator != null) { 
        double x = Double.parseDouble(arg1); 
        double y = Double.parseDouble(arg2); 
        double z = operator.execute(x, y); 
        System.out.println(String.format("%-10.4f %s %-10.4f = %-10.4f", x, oper, y, z)); 
       } else { 
        System.out.println(String.format("No such operator '%s'", oper)); 
       } 
      } catch (NumberFormatException e) { 
       e.printStackTrace(); 
      } 
     } while (operator != null); 
    } 

    public static String getInput(String prompt) { 
     System.out.print(prompt); 
     return sc.nextLine(); 
    } 
} 
Смежные вопросы