Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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 在查找不同运算符时使用字符串解析int_Java - Fatal编程技术网

Java 在查找不同运算符时使用字符串解析int

Java 在查找不同运算符时使用字符串解析int,java,Java,我对标题所暗示的事情有意见。我需要对字符串进行标记,确定用于求解它的操作,将数字转换为int类型并返回表达式 就解析和标记化而言,我到底做错了什么?在我尝试使用stringSplit之前,一切似乎都正常。唯一允许的库函数是Integer.parseInt()和split()。关于StackOverflow有很多帮助,但是没有一个不使用这两个库函数。以下是迄今为止我掌握的代码: public static void main(String[] args) { String a[] = {"

我对标题所暗示的事情有意见。我需要对字符串进行标记,确定用于求解它的操作,将数字转换为
int
类型并返回表达式

就解析和标记化而言,我到底做错了什么?在我尝试使用stringSplit之前,一切似乎都正常。唯一允许的库函数是
Integer.parseInt()
split()
。关于StackOverflow有很多帮助,但是没有一个不使用这两个库函数。以下是迄今为止我掌握的代码:

public static void main(String[] args)
{
    String a[] = {"12 + 34", "56 - 78", "99 * 99", "10 / 3"};
    stringSplit(a, ',');
}

public static int parseInt(String a)
{
    int i;
    int sum = 0;
    double x = Integer.parseInt(a);

    for(i = 0; i < a.length(); i++)
        sum = sum + x;

    System.out.printf("%s = %d\n", sum);
}
publicstaticvoidmain(字符串[]args)
{
字符串a[]={“12+34”、“56-78”、“99*99”、“10/3”};
stringSplit(a,,);
}
公共静态int-parseInt(字符串a)
{
int i;
整数和=0;
double x=整数.parseInt(a);
对于(i=0;i
最终结果应该类似于:

12+34=46.00

56-78=-22.00

诸如此类。我不是真的在寻找答案。更多的是我的答案。提前感谢您的帮助

公共类测试{
public class Test{
    public static void main(String[] args) {
        String a[] = { "12 + 34", "56 - 78", "99 * 99", "10 / 3" };
        parseInt(a);
    }

    public static void parseInt(String[] a) {
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            String[] pieces = a[i].split(" ");
            if("+".equals(pieces[1])){
                sum = Integer.valueOf(pieces[0]) + Integer.valueOf(pieces[2]);
            }else if("-".equals(pieces[1])){
                sum = Integer.valueOf(pieces[0]) - Integer.valueOf(pieces[2]);
            }else if("*".equals(pieces[1])){
                sum = Integer.valueOf(pieces[0]) * Integer.valueOf(pieces[2]);
            }else {
                sum = Integer.valueOf(pieces[0]) / Integer.valueOf(pieces[2]);
            }
            System.out.println("sum" + sum);

        }
    }
}
公共静态void main(字符串[]args){ 字符串a[]={“12+34”、“56-78”、“99*99”、“10/3”}; 帕西恩特(a); } 公共静态void parseInt(字符串[]a){ 整数和=0; for(int i=0;i
公共类测试{
公共静态void main(字符串[]args){
字符串a[]={“12+34”、“56-78”、“99*99”、“10/3”};
帕西恩特(a);
}
公共静态void parseInt(字符串[]a){
整数和=0;
for(int i=0;i
以下是您似乎正在尝试的工作版本。我在空格(
)上拆分输入数组的每个元素,然后提取出两个操作数和一个运算符。我还加入了一个被零除的检查

public static void main(String[] args) throws Exception {
    String a[] = {"12 + 34", "56 - 78", "99 * 99", "10 / 3"};
    stringProcess(a);
}

public static void stringProcess(String[] a) {
    for (int i=0; i < a.length; ++i) {
        String[] parts = a[i].split(" ");
        double operand1 = Double.parseDouble(parts[0]);
        String operator = parts[1];
        double operand2 = Double.parseDouble(parts[2]);
        double result = 0.0;

        switch (operator) {
            case "+":
                result = operand1 + operand2;
                break;

            case "-":
                result = operand1 - operand2;
                break;

            case "*":
                result = operand1 * operand2;
                break;

            case "/":
                if (operand2 == 0) {
                    throw new IllegalArgumentException("Divide by zero!");
                }
                result = operand1 / operand2;
                break;
        }

        System.out.println(operand1 + " " + operator + " " + operand2 +
            " = " + String.format( "%.2f", result));
    }
}

这是您似乎正在尝试的工作版本。我在空格(
)上拆分输入数组的每个元素,然后提取出两个操作数和一个运算符。我还加入了一个被零除的检查

public static void main(String[] args) throws Exception {
    String a[] = {"12 + 34", "56 - 78", "99 * 99", "10 / 3"};
    stringProcess(a);
}

public static void stringProcess(String[] a) {
    for (int i=0; i < a.length; ++i) {
        String[] parts = a[i].split(" ");
        double operand1 = Double.parseDouble(parts[0]);
        String operator = parts[1];
        double operand2 = Double.parseDouble(parts[2]);
        double result = 0.0;

        switch (operator) {
            case "+":
                result = operand1 + operand2;
                break;

            case "-":
                result = operand1 - operand2;
                break;

            case "*":
                result = operand1 * operand2;
                break;

            case "/":
                if (operand2 == 0) {
                    throw new IllegalArgumentException("Divide by zero!");
                }
                result = operand1 / operand2;
                break;
        }

        System.out.println(operand1 + " " + operator + " " + operand2 +
            " = " + String.format( "%.2f", result));
    }
}
按照方法语法。
(论点)。如果将int作为返回类型,则方法应返回一个值,否则,如果不需要任何返回值,则将返回类型设置为void。
根据方法语法。
(论点)。如果将int作为返回类型,则方法应返回一个值,否则,如果不需要任何返回值,则将返回类型设置为void。

stringSplit
方法的代码缺失。。如错误所示,您在
parseInt
中缺少返回语句。该方法应该有一个return语句,或者将其设置为
void
,而不是
int
。请修复这些基本错误并提出具体问题。您当前的问题到处都是,很难在其中找到具体的问题。@AniketKulkarni谢谢!“为了正确使用该方法,我现在正在研究这个问题。”MarkRotterveel我的返回语句问题已经得到了回答和修复。我没有发现一个简单的错误,很高兴你的眼睛看到了!在我的帖子中还有一个问号,那就是“就解析和标记化而言,我到底做错了什么?”缺少stringSplit方法的代码。正如错误所示,您缺少parseInt中的返回语句。该方法应该有一个return语句,或者将其设置为
void
,而不是
int
。请修复这些基本错误并提出具体问题。您当前的问题到处都是,很难在其中找到具体的问题。@AniketKulkarni谢谢!“为了正确使用该方法,我现在正在研究这个问题。”MarkRotterveel我的返回语句问题已经得到了回答和修复。我没有发现一个简单的错误,很高兴你的眼睛看到了!在我的帖子中还有一个问号,那就是“就解析和标记化而言,我到底做错了什么?”非常好!然而,由于这是家庭作业,我将看看我是否能找到另一种方法来超越案例陈述。谢谢你,好先生太好了!然而,由于这是家庭作业,我将看看我是否能找到另一种方法来超越案例陈述。谢谢你,好先生D
As per method syntax.
<Access specifier><modifier><return type><method name>(arguments). If you are giving int as return type your method should return a value, else make the return type as void if you do not need any return value.