Note that there are some explanatory texts on larger screens.

plurals
  1. POResult based on UserInput
    primarykey
    data
    text
    <p>I've been learning about enums and switch statements and I have the following code</p> <p>I dont want to run arguments through code, then compile it to see the result, I want a user to input numbers into terminal and +,x,/,- and to get a result back in terminal, how can I accomplish this</p> <pre><code>public class MathCalculatorTest2_0 { MathFunctions1_0 mathFunction1; public MathCalculatorTest2_0 (MathFunctions1_0 mathFunction1) { this.mathFunction1 = mathFunction1; } public void DoMath(double digit1, double digit2) { double mathResult = 0; switch (mathFunction1) { case MULTIPLY: mathResult = digit1*digit2; break; case DIVIDE: mathResult = digit1/digit2; break; case ADD: mathResult = digit1+digit2; break; case SUBTRACT: mathResult = digit1-digit2; break; default: System.out.println("Incorrect arithmatic character, please insert a correct arithmatic character between the two numbers (x, /, +, -)"); } System.out.println(mathResult); } public static void main(String[] args) { if (args.length &lt; 3) { System.out.println("Please correct syntax"); } else { double firstDigit = Double.parseDouble(args[0]); String userInput1 = args[1]; double secondDigit = Double.parseDouble(args[2]); MathCalculatorTest2_0 C; switch (userInput1) { case "x": C = new MathCalculatorTest2_0(MathFunctions1_0.MULTIPLY); C.DoMath(firstDigit, secondDigit); break; case "/": C = new MathCalculatorTest2_0(MathFunctions1_0.DIVIDE); C.DoMath(firstDigit, secondDigit); break; case "+": C = new MathCalculatorTest2_0(MathFunctions1_0.ADD); C.DoMath(firstDigit, secondDigit); break; case "-": C = new MathCalculatorTest2_0(MathFunctions1_0.SUBTRACT); C.DoMath(firstDigit, secondDigit); break; default: System.out.println("Please insert correct arithmatic character."); } } } </code></pre> <p>}</p> <pre><code>public enum MathFunctions1_0 { MULTIPLY, DIVIDE, ADD, SUBTRACT </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload