-2

Sup Guys,

Need help which is confusing. I have this calculator made using some methods. I would like to know if what I have done is a good way of writing it or is there any other better ways to do it. 2nd question is if you see the SWITCH CASE 5, if the user is prompted for Y/N the user can input various characters not sticking only to Y and N....how can I Limit the user to use only Y and N.

****THIS IS MY MAINCALC JAVA FILE******

package calculator;

import java.util.Scanner;

public class MainCalc {

public  static  void    main    (String args []) {

    Scanner scan = new Scanner(System.in);

    Calcmethod calc = new Calcmethod();

    char choice;


    do {
    System.out.println("<*-*-*-*-*-*>");
    System.out.println("1. Add ");
    System.out.println("2. Sub ");
    System.out.println("3. Mul ");
    System.out.println("4. Div ");
    System.out.println("5. Exit");
    System.out.println("<*-*-*-*-*-*>\n");

    choice = scan.next().charAt(0);     

    switch (choice){

    case '1': System.out.println("The CalcVal is:  " + "" + calc.calcAdd());
    break;

    case '2': System.out.println("The CalcVal is:  " + "" + calc.calcSub());
    break;

    case '3': System.out.println("The CalcVal is:  " + "" + calc.calcMul());
    break;

    case '4': System.out.println("The CalcVal is:  " + "" + calc.calcDiv());
    break;

    case '5': System.out.println("Are You Sure y/n??");
                choice = scan.next().charAt(0);     
                if (choice !='n') {
                System.out.println("Thanks for using Calc!!!");
                System.exit(0);
                }
                break;
    default: System.out.println("Wrong Choice of Selection - Please Select between 1-5 only");
    break;

        }//switch
    }while (choice != '5');
    scan.close();
}
}


****THIS IS MY CALCMETHOD JAVA FILE******

package calculator;

import java.util.Scanner;

public class Calcmethod {

Scanner scan = new Scanner(System.in);




public double calcAdd(){
    System.out.println("Enter 1st Val:  ");
    double x = scan.nextDouble();

    System.out.println("Enter 2nd Val:  ");
    double y = scan.nextDouble();

    double res = (x+y);
    return res;
}

public double calcSub(){
    System.out.println("Enter 1st Val:  ");
    double x = scan.nextDouble();

    System.out.println("Enter 2nd Val:  ");
    double y = scan.nextDouble();

    double res = (x-y);
    return res;
}

public double calcMul(){
    System.out.println("Enter 1st Val:  ");
    double x = scan.nextDouble();

    System.out.println("Enter 2nd Val:  ");
    double y = scan.nextDouble();

    double res = (x*y);
    return res;
}

public double calcDiv(){
    System.out.println("Enter 1st Val:  ");
    double x = scan.nextDouble();

    System.out.println("Enter 2nd Val:  ");
    double y = scan.nextDouble();

    double res = (x/y);
    return res;
}
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
FJ-
  • 1
  • 2

1 Answers1

-1

You can't limit user input. You can only anticipate and have your program be ready for any inputs that may cause errors by using Exception Handling.

dranreb dino
  • 260
  • 2
  • 17