【问题标题】:Repeating the main method重复主要方法
【发布时间】:2016-11-30 22:57:49
【问题描述】:

我是 Java 和这个论坛的新手。我为一个简单的计算器编写了代码。它正在工作。但是,如果我(假设)输入“=”而不是“(+,-,*,/)”,我该如何重复主要方法?我应该使用循环还是其他?提前致谢!

import java.util.Scanner;

public class SimCal {

    public static int add(int a, int b) {
        return a + b;
    }

    public static int sub(int a, int b) {
        return a - b;
    }

    public static int mul(int a, int b) {
        return a * b;
    }

    public static int div(int a, int b) {
        return a / b;
    }

    public static void main(String[] args) {

        Scanner scan1 = new Scanner(System.in);
        System.out.println("What do you want to do (+, -, *, /)? ");
        String input1 = scan1.nextLine();

        if (!input1.equals("+") && !input1.equals("-") && !input1.equals("*") && !input1.equals("/")) { // if wrong input given
            System.out.println("You must Enter a valid operator");
        } else {
            Scanner scan2 = new Scanner(System.in);
            System.out.println("Enter first number: ");
            int input2 = scan2.nextInt();

            Scanner scan3 = new Scanner(System.in);
            System.out.println("Enter second number: ");
            int input3 = scan3.nextInt();

            if (input1.equals("+")) {
                System.out.println(add(input2, input3));
            } else if (input1.equals("/")) {
                System.out.println(div(input2, input3));
            } else if (input1.equals("-")) {
                System.out.println(sub(input2, input3));
            } else {
                System.out.println(mul(input2, input3));
            }
            scan1.close();
            scan2.close();
            scan3.close();
        }
    }
}

【问题讨论】:

  • 在while循环中使用switch case。
  • 不要创建多个扫描仪。重复使用scan1 代替scan2scan3

标签: java main-method


【解决方案1】:

我有点不确定你在问什么,但我理解你希望能够重复计算器而不必再次运行它。这可以通过使用 boolean 和 while 块来实现。

这是一个例子:

import java.util.Scanner;

public class SimCal {

    public static int add (int a, int b){
        return a+b;
    }
    public static int sub (int a, int b){
        return a-b;
    }
    public static int mul (int a, int b){
        return a*b;
    }
    public static int div (int a, int b){
        return a/b;
    }
    public static boolean done = false;


    public static void main(String[] args){


        Scanner scan1 = new Scanner(System.in);
        Scanner scan2 = new Scanner(System.in);
        Scanner scan3 = new Scanner(System.in);
        while (!done) {
            System.out.println("What do you want to do (+, -, *, /, quit)? ");
            String input1 = scan1.nextLine();

            if (!input1.equals("+") && !input1.equals("-") && !input1.equals("*") && !input1.equals("/") && !input1.equals("quit")) 
            { //if wrong input given
                System.out.println("You must Enter a valid operator");
            } 
            else if (input1.equals("quit")) 
            {
                done = true;
                scan1.close();
                scan2.close();
                scan3.close();
            } 
            else 
            {
                System.out.println("Enter first number: ");
                int input2 = scan2.nextInt();


                System.out.println("Enter second number: ");
                int input3 = scan3.nextInt();

                if (input1.equals("+")) 
                {
                    System.out.println(add(input2, input3));
                } 
                else if (input1.equals("/")) 
                {
                    System.out.println(div(input2, input3));
                } 
                else if (input1.equals("-")) 
                {
                    System.out.println(sub(input2, input3));
                } 
                else 
                {
                    System.out.println(mul(input2, input3));

                }

            }
        }
    }
}

我希望这会有所帮助。就像 Andy Turner 提到的那样,您应该尽量不要使用多个扫描仪。

编辑:我忘记关闭 2 个扫描仪。此外,正如 Saurav Sahu 所说,switch case 可能是一种更好的方法。

【讨论】:

  • 谢谢,我就是这个意思。
猜你喜欢
  • 2018-03-05
  • 2020-06-20
  • 2016-07-20
  • 1970-01-01
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 2018-03-21
相关资源
最近更新 更多