【问题标题】:Java in/output resultJava 输入/输出结果
【发布时间】:2016-12-10 08:32:23
【问题描述】:

我是 java se 的新手,我遇到了一些问题。 下面的代码应该是一个计算器。它工作正常但是。要计算输出,扫描仪类的输入应该像这样输入:
1:输入第一个数字 - 按键盘上的 Enter 键
2:输入大小写 +-* 或 / 然后再次在键盘上按 enter
3:输入第二个数字并再次按回车键
4:终于输出了

原来是这样的

计算器

2

-

1

计算为1:

问题是有没有办法把它放在一行中


1.输入两个数字 2-1 - 按键盘上的回车键
2。输出显示为 Calculation: 1

public class Calculator {

    public static void main(String... args) {

        int num1;
        int num2;

        Scanner scan = new Scanner(System.in);
        System.out.println("Calculator");

        num1 = scan.nextInt();
        String str = scan.next();
        num2 = scan.nextInt();

        System.out.print(("Calculation is " + calculate(num1, str, num2)));
        scan.close();

    }

    private static int calculate(int num1, String str, int num2) {

        switch (str.charAt(0)) {
        case '+':
            return num1 + num2;
        case '-':
            return num1 - num2;
        case '*':
            return num1 * num2;
        case '/':
            return num1 / num2;

        }
        return calculate(num1, str, num2);

    }
}

【问题讨论】:

标签: java arrays


【解决方案1】:

如果您只想将用户的输入显示在一行中,那么您可以轻松实现这一点,而无需更改当前实现中的一行代码。您只需将输入与空格分开即可。

所以不要使用 2-1,而是使用 2 - 1

扫描器使用分隔符模式将其输入分解为标记,默认情况下匹配空格。然后可以使用各种 next 方法将生成的标记转换为不同类型的值... Scanner Java Documentation

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    是的,这是可能的,这样做的方法是使用一个字符串来捕获整个操作,然后将其分解为不同的部分。`public class application {

    public static void main(String[] args) {
    
    
    
        // get the operation from the user
        Scanner scan = new  Scanner(System.in);
    
        System.out.print("Enter your operation : ");
    
        String operation = scan.nextLine();
    
    
        // booleans to evaluate to true if a certain sign is use
        boolean isAddition = operation.contains("+");
        boolean isSubstration = operation.contains("-");
        boolean isDivision = operation.contains("/");
        boolean isMultiplication = operation.contains("*");
    
        if(isAddition){       
            // get result from the function in charge of processing the operation 
           int result = processOperation(operation, '+');
           //output the result
           System.out.println(" = "+result);
        }
        if(isSubstration){     
           // get result from the function in charge of processing the operation
           int result = processOperation(operation, '-');
           //output the result
           System.out.println(" = "+result);
        }
        if(isDivision){ 
        // get result from the function in charge of processing the operation 
           int result = processOperation(operation, '/');
           //output the result
           System.out.println(" = "+result);
        }
        if(isMultiplication){   
           // get result from the function in charge of processing the operation
           int result = processOperation(operation, '*');
           //output the result
           System.out.println(" = "+result);
        }
    

    }

    private static int processOperation(字符串操作,字符符号){

            //remove all the whitespace in the string
            operation = operation.replaceAll("\\s+", "");
    
            // get the position of the sign operator in the String
            int signIndex = operation.indexOf(sign);
    
            //get the first number from the string which start from index 0 up to signIndex 
            int num1 = Integer.parseInt(operation.substring(0, signIndex));
    
            //get the second number from the string which is start from signIndex 
            int num2 = Integer.parseInt(operation.substring(signIndex+1));
    
    
            //evaluate the sign in use in order to perfom the appropriate operation 
        if (sign == '+') {
            return num1 + num2;
        } else if (sign == '-') {
            return num1 - num2;
        } else if (sign == '*') {
            return num1 * num2;
        } else {
            return num1 / num2;
        }
    }
    

    }`

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多