将字符串转换为整数

时间:2019-08-14 11:11:36

标签: java math input

我正在努力实现的目标:给定一个简单的方程,您应该为变量“ x”输出正确的值。该方程式具有两个大于0的数字和变量“ x”,并且它们之间可以是“ +”,“-”或“ =”。数字,变量“ x”,符号“ +”,“-”,“ =”均用空格分隔。

问题:当我读取数字时,它们以字符串而不是整数的形式出现

我对编码非常陌生,并尝试了此方法,但是我不明白为什么当我使用var并输入数字时,它会将其读取为字符串而不是整数

import java.util.*;

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

    int outpt;
    Scanner scanner = new Scanner(System.in);
    var sig1 = scanner.next();
    var sig2 = scanner.next();
    var sig3 = scanner.next();
    var sig4 = scanner.next();
    var sig5 = scanner.next();

    if(sig2 == "=")
    {
        if(sig1 == (int)sig1)
        {
            if(sig3 == (int)sig3)
            {
                if(sig4 == "-")
                {
                    outpt = sig3 - sig1;
                    System.out.println("");
                    System.out.println(outpt);
                }
                else
                {
                    outpt = sig1 - sig3;
                    System.out.println("");
                    System.out.println(outpt);
                }
            }
            else
            {
                if(sig4 == "-")
                {
                    outpt = sig5 + sig1;
                    System.out.println("");
                    System.out.println(outpt);
                }
                else
                {
                    outpt = sig1 - sig5;
                    System.out.println("");
                    System.out.println(outpt);
                }
            }
        }
        else
        {
            if(sig4 == "-")
                {
                    outpt = sig3 - sig5;
                    System.out.println("");
                    System.out.println(outpt);
                }
                else
                {
                    outpt = sig3 + sig5;
                    System.out.println("");
                    System.out.println(outpt);
                }
        }
    }
    else
    {
        if(sig5 == (int)sig5)
        {
            if(sig1 == (int)sig1)
            {
                if(sig2 == "+")
                {
                    outpt = sig5 - sig1;
                    System.out.println("");
                    System.out.println(outpt);
                }
                else
                {
                    outpt = sig1 - sig5;
                    System.out.println("");
                    System.out.println(outpt);

                }
            }
            else
            {
                if(sig2 == "+")
                {
                    outpt = sig5 - sig3;
                    System.out.println("");
                    System.out.println(outpt);
                }
                else
                {
                    outpt = sig3 + sig5;
                    System.out.println("");
                    System.out.println(outpt);

                }
            }
        }
        else
        {
            if(sig2 == "+")
            {
                outpt = sig1 + sig3;
                System.out.println("");
                System.out.println(outpt);
            }
            else
            {
                outpt = sig1 - sig3;
                System.out.println("");
                System.out.println(outpt);
            }
        }
    }

    }
}

样本输入1:

5 + x = 15 样本输出1:

10

样本输入2:

x-8 = 10 样本输出2:

18

样本输入3:

x = 20-15 样本输出3:

5

2 个答案:

答案 0 :(得分:0)

代替使用100 if语句尝试此操作。在我的代码中,我将整个方程式作为输入,并将其存储在数组中,然后将符号和数字的位置保存在变量中,以便以后使用来决定应执行的操作

这是代码

import java.util.Scanner;

public class AlgebraEquation {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the whole equation: ");
        String equation = scan.nextLine();
        scan.close();

        String[] equationSplit = equation.split(" ");

        // This code block is just to show how logic is working
        for (int i = 0; i < equationSplit.length; i++) {
            System.out.print(equationSplit[i] + ", ");
        }
        System.out.println();

        int xPosition = -1, const1Pos = -1, const2Pos = -1, equalPos = -1, operatorPos = -1;

        // Checking each index of array and saving position of characters
        for (int i = 0; i < equationSplit.length; i++) {
            if (equationSplit[i].equalsIgnoreCase("X")) {
                xPosition = i;
            } else if (equationSplit[i].equals("=")) {
                equalPos = i;
            } else if (equationSplit[i].matches("[-+*/]")) { // If character matches any of these "-+*/"
                operatorPos = i;
            } else {
                // Checking if const1Pos is filled or not
                if (const1Pos < 0) {
                    const1Pos = i;
                } else {
                    const2Pos = i;
                }
            }
        }

        // This code block is just to show how logic is working
        System.out.println(equationSplit[xPosition] + " Position: " + xPosition);
        System.out.println(equationSplit[equalPos] + " Position: " + equalPos);
        System.out.println(equationSplit[operatorPos] + " Position: " + operatorPos);
        System.out.println(equationSplit[const1Pos] + " Position: " + const1Pos);
        System.out.println(equationSplit[const2Pos] + " Position: " + const2Pos);

        String operator = equationSplit[operatorPos];
        switch (operator) {
        case "+":
            System.out.println("Addition");
            // Do you calculation based on relative position of equal, const1Pos, and
            // const2Pos
            break;
        case "-":
            System.out.println("Subtraction");
            break;
        case "*":
            System.out.println("Multiplication");
            break;
        case "/":
            System.out.println("Division");
            break;
        }
    }
}
  

输出

Enter the whole equation: 5 + x = 8 //(Enter Key Pressed)
5, +, x, =, 8, 
x Position: 2
= Position: 3
+ Position: 1
5 Position: 0
8 Position: 4
Addition

答案 1 :(得分:-1)

这是最终的代码

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String a = scanner.next();
        String b = scanner.next();
        String c = scanner.next();
        String d = scanner.next();
        String e = scanner.next();

        if (b.equals("+") && a.equals("x")) {
            System.out.println(Integer.parseInt(e) - Integer.parseInt(c));
        } else if (b.equals("+") && c.equals("x")) {
            System.out.println(Integer.parseInt(e) - Integer.parseInt(a));
        } else if (b.equals("-") && a.equals("x")) {
            System.out.println(Integer.parseInt(e) + Integer.parseInt(c));
        } else if (b.equals("-") && c.equals("x")) {
            System.out.println(Integer.parseInt(a) - Integer.parseInt(e));
        } else if (d.equals("+") && c.equals("x")) {
            System.out.println(Integer.parseInt(a) - Integer.parseInt(e));
        } else if (d.equals("+") && e.equals("x")) {
            System.out.println(Integer.parseInt(a) - Integer.parseInt(c));
        } else if (d.equals("-") && c.equals("x")) {
            System.out.println(Integer.parseInt(a) + Integer.parseInt(e));
        } else if (d.equals("-") && e.equals("x")) {
            System.out.println(Integer.parseInt(c) - Integer.parseInt(a));
        } else if (b.equals("+") && e.equals("x")) {
            System.out.println(Integer.parseInt(c) + Integer.parseInt(a));
        } else if (d.equals("+") && a.equals("x")) {
            System.out.println(Integer.parseInt(c) + Integer.parseInt(e));
        } else if (b.equals("-") && e.equals("x")) {
            System.out.println(Integer.parseInt(a) - Integer.parseInt(c));
        } else if (d.equals("-") && a.equals("x")) {
            System.out.println(Integer.parseInt(c) - Integer.parseInt(e));
        }
    }
}