数学字符串没有空格?

时间:2013-10-09 01:12:47

标签: java string conditional-statements

import java.util.Scanner;

public class Improved {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter a number operaion number: "); 
        int operand1 = Integer.parseInt(input.nextLine());
        char expo1 = input.next().charAt(0);
        int operand2 = Integer.parseInt(input.nextLine());

        System.out.println( operand1 + expo1 + operand2 + "=");

        if ( expo1 == '/'  && operand2 == '0' ){
            System.out.println("Cannot divide by zero"); }
        else
            if (expo1 == '-') {
                System.out.println(operand1-operand2);
            } else 
            if (expo1 == '+') {
                System.out.println(operand1+operand2);
            } else
            if (expo1 == '/') {
                System.out.println(operand1/operand2);
            } else 
            if (expo1 == '%') {
                System.out.println(operand1%operand2);
            }
            else{
                System.out.println(" Error.Invalid operator.");
            }
    }
}

//This bottom works, but I found out that this is not what is supposed to be done with this problem

/*
public class Else {
    public static void main(String[] args) {

        int operand1;
        char exp1;
        int operand2;

        if (args.length != 3 ) {
            System.err.println("*** Program needs 3 arguements***");
            System.err.println("Usage: java Else int1 exp int2");
            System.exit(1);
        }

        operand1 = Integer.parseInt(args[0]);

        exp1 = args[1].charAt(0);

        operand2 = Integer.parseInt(args[2]);


        System.out.print(args[0] + args[1] + args[2] + "=");

        if(exp1 == '-') {
            System.out.println(operand1 - operand2);
        } else 
        if (exp1 == '+') {
            System.out.println(operand1 + operand2);
        } else
        if (exp1 == '/') {
            System.out.println(operand1 / operand2);
        } else 
        if (exp1 == '%') {
            System.out.println(operand1 % operand2);
        }
        else{
            System.out.println(" Error.Invalid operator.");
        }
    }
}
*/

我想让程序做的是让一个人进入数学运算1/2或1%2(不是乘法) ,但就像没有空格的那样。不过,我想检查哪些操作正在进行,这就是我放置if语句的原因。我没有得到的是程序在字符串中出现操作时如何知道。我甚至不确定我是否正确设置它。总的来说,我想要一个字符串,然后读取数字然后再操作数字。我很抱歉,如果这似乎是在做我的hw,但我已经尝试多次制作这个程序,但是无法理解我怎么能用字符串做到这一点。我写了第二个来表明我已多次这样做,所以你可以忽略它。非常感谢你!

4 个答案:

答案 0 :(得分:0)

使用以下命令将输入​​读取为String:

String inputString = input.nextLine();

获取运算符的索引:

int indexOp = inputString.indexOf("+");
if(indexOp < 0) indexOp = inputString.indexOf("-"); //cannot find +, so find -
if(indexOp < 0) indexOp = inputString.indexOf("/"); //cannot find -, so find /
if(indexOp < 0) indexOp = inputString.indexOf("%"); //cannot find /, so find %

获取第一个和第二个操作数:

int operand1 = Integer.parseInt(inputString.substring(0,indexOp));
int operand2 = Integer.parseInt(inputString.substring(indexOp+1,inputString.length());

从我们之前获得的indexOp中获取运算符:

char operator = inputString.charAt(indexOp);

希望有所帮助:)

答案 1 :(得分:0)

我毫不怀疑有多种方法可以实现,这只是另一个例子......

这试图做的是将传入的文本分解为数字和非数字组。然后循环通过这些组构成计算的各种元素......

Scanner input = new Scanner(System.in);
System.out.println("Enter a number operaion number: ");
String text = input.nextLine();
System.out.println("Input = " + text);
text = text.replaceAll("\\s", "");
System.out.println("Parse = " + text);

Pattern p = Pattern.compile("\\d+|\\D+");
Matcher m = p.matcher(text);
int index = 0;
int op1 = -1;
int op2 = -2;
String exp1 = "";
while (index < 3 && m.find()) {
    System.out.println(index);
    String part = m.group();
    switch (index) {
        case 0:
            op1 = Integer.parseInt(part);
            break;
        case 2:
            op2 = Integer.parseInt(part);
            break;
        case 1:
            exp1 = part;
            break;
    }
    index++;
}

System.out.println(op1 + " " + exp1 + " " + op2);

它具有的功能是允许您提供更长的计算,例如20+30/40-50 ......等。

您需要将每个操作数和指数停放到某种List并根据需要提取它们......或者您实际上可以直接在while循环中进行计算

答案 2 :(得分:0)

试试这个:

package week11;

import java.util.Scanner;

public class maths {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("enter a number "); 


        int x = scanner.nextInt();
        System.out.println("put +, -, / or * ");
        char expo1 = scanner.next().charAt(0);
        System.out.println("second number please ");
        int y = scanner.nextInt();


        System.out.println( "Answer is" + ":");

        if ( expo1 == '/'  && y == '0' ){
            System.out.println("cannot be divided by 0"); }
        else
            if (expo1 == '-') {
                System.out.println(x-y);
            } else 
            if (expo1 == '+') {
                System.out.println(x+y);
            } else
            if (expo1 == '/') {
                System.out.println(x/y);
            } else 
            if (expo1 == '%') {
                System.out.println(x%y);
            }
            else{
                System.out.println(" Error!");
            }
    }
}

答案 3 :(得分:0)

我想添加另一个解决方案,它可以删除大量的解析工作。

import java.util.Scanner;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

class Scratch {
    public static void main(String[] args) throws ScriptException {
        System.out.println("Enter an operation:");
        Scanner input = new Scanner(System.in);
        String operation = input.nextLine();

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        Object result = engine.eval(operation);
        System.out.printf("%s = %s%n", operation, result);
    }
}

示例结果

Enter an operation:
2 + 3 * 4
2 + 3 * 4 = 14.0