简体   繁体   中英

How can i make this code simpler? Also how can i check if input is an int?

Hi im a beginner in Java. How could i make this bais calculator simpler? Also, i want the user to get an error if num1 or num2 arent Integers. How could i do that?

I already tried parseint and scanner.hasnextint but couldnt get them to work.

import java.util.Scanner;

public class topfirst{
    public static void main(String[] args) {
        int num1;
        int num2;
        int result;
        String yesorno;
        String yes = "yes";
        String op;
        while(yesorno.equals(yes)){

            System.out.println("Please enter the first number :");
            Scanner inputnum1 = new Scanner(System.in);
            num1 = inputnum1.nextInt();            

            System.out.println("Please enter the second number :");
            Scanner inputnum2 = new Scanner(System.in);
            num2 = inputnum2.nextInt();

            System.out.println("Please enter an operation :");
            Scanner inputop = new Scanner(System.in);
            op = inputop.next();
            String minus = "-";
            String plus = "+";
            String multipl = "*";
            String div = "/";
            while ((!op.equals(minus)) && (!op.equals(plus)) && (!op.equals(multipl)) && (!op.equals(div)))
            {
                System.out.println("Incorrent operation, try again.");
                op = inputop.next();
            }
            switch(op){
                case "+":
                    result = num1 + num2;
                    System.out.println(num1 + " + " + num2 + " = " + result);
                    break;
                case "-":
                    result = num1 - num2;
                    System.out.println(num1 + " - " + num2 + " = " + result);
                    break;
                case "*":
                    result = num1 * num2;
                    System.out.println(num1 + " * " + num2 + " = " + result);
                    break;
                case "/":
                    result = num1 / num2;
                    System.out.println(num1 + " / " + num2 + " = " + result);
                    break;


            }
         System.out.println("Do you want to calculate something else?");
         Scanner inyesorno = new Scanner(System.in);
         yesorno = inyesorno.next();
        }}
}

One Error that needs fixing before anything else:

 String yesorno;
 String yes = "yes";
 String op;
 while(yesorno.equals(yes)){
     [...]
 }

Your String yesorno does NOT equal "yes", so the while-loop is never executed in the first place!

import java.util.Scanner;

public class TopFirst {

    private static final String PLUS = "+";
    private static final String MINUS = "-";
    private static final String MULTIPLY = "*";
    private static final String DIV = "/";

    private static final Scanner INPUT = new Scanner(System.in);

    public static void main(String[] args) {

        do {

            System.out.print("Please enter the first number: ");

            int num1 = readInt();

            System.out.print("Please enter the second number: ");

            int num2 = readInt();

            System.out.print("Please enter an operator: ");

            String operator = readOperator();

            int result = countResult(num1, num2, operator);
            System.out.printf("%d %s %d = %d%n", num1, operator, num2, result);

            System.out.println("Do you want to calculate something else?");

        } while (INPUT.next().equals("yes"));
    }

    private static String readOperator() {
        String operator = INPUT.next();

        if (isCorrect(operator)) {
            return operator;
        } else {
            System.out.print("Incorrect operator, try again: ");
            return readOperator();
        }
    }

    private static int readInt() {
        try {
            return Integer.parseInt(INPUT.next());
        } catch (NumberFormatException e) {
            System.out.print("Incorrect number, try again: ");
            return readInt();
        }
    }

    private static boolean isCorrect(String operator) {
        return MINUS.equals(operator)
            || PLUS.equals(operator)
            || MULTIPLY.equals(operator)
            || DIV.equals(operator);
    }

    private static int countResult(int num1,
                                   int num2,
                                   String operator) {
        switch (operator) {
            case PLUS:
                return num1 + num2;
            case MINUS:
                return num1 - num2;
            case MULTIPLY:
                return num1 * num2;
            case DIV:
                return num1 / num2;
            default:
                return 0;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM