dimanche 30 octobre 2016

Program runs unpredictably with the exact same input values

I'm new to programming and java and have an assignment that I've been able to run properly- sometimes, but it is inconsistent. I ran it with certain values once and then directly tried re-running it with the same exact input as before. The next time or so it doesn't work. Please help point me to how to fix this unreliability. I can't find any reasons for this in my code. Please help.

import java.util.*; 
public class Calculator {

    public static void main(String[] args) { //void
        boolean done = false;
        Scanner console = new Scanner(System.in);
        while (!done){
            displayMenu();
            String selection = getUsersSelection(console);
            done = processSelection(selection, console);
            //caluculateResults(console);
        }
        System.out.println("Thank you for using this program");
    }

    private static boolean processSelection(String selection, Scanner console) {
        boolean done = false;
        if (!selection.equalsIgnoreCase("Q")){
            if (selection.equalsIgnoreCase("U")){
                caluculateResults(console);
                }
            else if (selection.equalsIgnoreCase("H")){

            }
            else {
                System.out.println("Incorrect entry...try again!");
            }
        }
        else {
            done = true;
        }
        return done;
    }

    private static void caluculateResults(Scanner console) {
        displayCalculatorInstructions();
        double operand1 = console.nextDouble();
        char operator = console.next().charAt(0);
        double operand2 = console.nextDouble();
        double result = 0.00; //double from int
        boolean isOperatorValid = true;
        if (operator == '+'){
            result = operand1 + operand2;
        }
        else if (operator == '-'){
            result = operand1 - operand2; 
}
        else if (operator == '*'){
            result = (double) operand1 * (double) operand2;
        }
        else if (operator == '/'){
            if (operand2 != 0.00){
                result = operand1 / operand2;
            }
            else {
                result = (int)Double.NaN;
            }
        }
        else if (operator == '^'){
            result = Math.pow(operand1, operand2);
        }
        else {
            isOperatorValid = false;
            System.out.println("Invalid operator");
}
        if (isOperatorValid){
            System.out.println("The result of your operation is: ");
            System.out.printf(operand1 +" "+operator+" "+operand2+" = %5.3f", result); 
            System.out.println();
        }
    }

    private static void displayCalculatorInstructions() {
        System.out.println("Enter a mathematical expression to evaluate");
        System.out.println("Valid operations are: +, -, /, *, ^ for power");
        System.out.println("Expression are entered with spaces between the values and operator");
        System.out.println("Here is the valid format:");
        System.out.println("\t<value><space><operator><space><value>");
        System.out.print("Your expression: ");
    }


    private static String 
    private static void displayMenu() {
        System.out.println("Enter one these options:");
        System.out.println("\tH for Help");
        System.out.println("\tU for using calculator");
        System.out.println("\tQ for exiting this program");
        System.out.print("Your selection: ");getUsersSelection(Scanner console) {
        String selection = console.next();
return selection;
    }

    }

}

Aucun commentaire:

Enregistrer un commentaire