Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java输入/输出结果_Java_Arrays - Fatal编程技术网

Java输入/输出结果

Java输入/输出结果,java,arrays,Java,Arrays,我是JavaSE的新手,我遇到了一些问题。 下面的代码应该是一个计算器。它工作得很好,但是。要计算输出,扫描仪类的输入应如下所示: 1:输入第一个数字-按键盘上的enter键 2:输入大小写+-*或/然后再次按回车键 3:输入第二个数字,然后再次按enter键 4:最后是输出 看起来是这样的 计算器 二, - 一, 计算结果为1: 问题是有没有办法把它放在一行 1。输入两个数字2-1-按键盘上的Enter键 二,。输出显示如下计算:1 public class Calculator {

我是JavaSE的新手,我遇到了一些问题。 下面的代码应该是一个计算器。它工作得很好,但是。要计算输出,扫描仪类的输入应如下所示:
1:输入第一个数字-按键盘上的enter键
2:输入大小写+-*或/然后再次按回车键
3:输入第二个数字,然后再次按enter键
4:最后是输出

看起来是这样的

计算器

二,

-

一,

计算结果为1:

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


1。输入两个数字2-1-按键盘上的Enter键
二,。输出显示如下计算: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);

    }
}

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

因此,与其使用2-1,不如使用2-1

扫描器使用定界符模式将其输入拆分为标记,默认情况下,定界符模式匹配空白。然后,可以使用各种后续方法将生成的令牌转换为不同类型的值。。。


希望这有帮助

是的,这是可能的,方法是使用字符串捕获整个操作,然后将其分解为不同的部分{

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);
    }
}

私有静态int-processOperation(字符串操作、字符符号){


}`

据我所知,可能的副本不是副本。他希望用户输入显示在一行中,您已经可以这样做了,只需在每个部分之间划出一个空格,如
2-1
        //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;
    }
}