简体   繁体   English

Java输入/输出结果

[英]Java in/output result

I am quite new learner of java se, and i am coming to some problems. 我是Java SE的新手,我遇到了一些问题。 The code below should be a calculator. 下面的代码应该是一个计算器。 It works fine But. 它工作正常,但是。 To calculate the output the input of the scanner class should be entered like this: 要计算输出,应按以下方式输入扫描仪类的输入:
1: enter 1st number - hit enter key on the keyboard 1:输入第一个数字-按键盘上的Enter键
2: enter the case +-* or / then again hit the enter in keyboard 2:输入大小写+-*或/,然后再次按键盘上的Enter
3: enter the second number and again hit the enter key 3:输入第二个数字,然后再次按Enter键
4: Finally comes the output 4:最后是输出

so it looks like this 所以看起来像这样

Calculator 计算器

2 2

- -

1 1

Calculation is 1: 计算为1:

The question is is there a way to put it in a single row 问题是有没有办法将其放在一行中


1. Enter the both numbers 2-1 - hit the enter on the keyboard 1.输入两个数字2-1-按键盘上的Enter键
2. The output shows like Calculation: 1 2.输出显示为计算: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);

    }
}

If all you want is the user's input to be displayed in a single line, then you can easily achieve this without changing a single line of code in your current implementation. 如果您只希望将用户输入显示在一行中,那么您可以轻松实现此目的,而无需在当前实现中更改一行代码。 You just simply need to separate your input with whitespace. 您只需要用空格分隔输入即可。

So rather than 2-1, simply use 2 - 1 因此,而不是2-1,只需使用2-1

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. 扫描程序使用定界符模式将其输入分为令牌,默认情况下,该模式与空格匹配。 The resulting tokens may then be converted into values of different types using the various next methods... Scanner Java Documentation 将得到的令牌然后可以使用各种方法下被转换成不同类型的值... 扫描仪Java文档

Hope this helps! 希望这可以帮助!

yes it is possible, the way to do that is to use a string to capture the whole operation then break it into different part.`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(String operation, char sign){ 私有静态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;
    }
}

}` }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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