Java 计算器错误,只输出方程,不回答

Java 计算器错误,只输出方程,不回答,java,debugging,calculator,Java,Debugging,Calculator,当运行代码并输入输入时,本应求解方程的输出仅打印先前输入的方程。请注意,代码中的注释在很大程度上是不正确的,因为它们来自代码的早期迭代 public static void main(String[] args){ // introduces the user to the programme System.out.println("Welcome to my calculator test"); System.out.println("Function inputs;

当运行代码并输入输入时,本应求解方程的输出仅打印先前输入的方程。请注意,代码中的注释在很大程度上是不正确的,因为它们来自代码的早期迭代

public static void main(String[] args){

    // introduces the user to the programme
    System.out.println("Welcome to my calculator test");
    System.out.println("Function inputs; + to add numbers, - to subtract numbers, * to multiply, / to devide, ^ to power,# to square root.");

    //creates a Scanner
    Scanner reader0 = new Scanner(System.in);
    //promts the user for an input
    System.out.println("Please enter first number"); 
    String a = reader0.nextLine();
    reader0.close();
    char test = 'x';

    String test1 = "";
    for (int i = 0; i < a.length (); i++ ){
        test = a.charAt(i);
        if (Character.isDigit(test) || test == ('.')){
            System.out.println(test);
            test1 = test1+""+test;
        }
        else if (test == '+' ||test == '-' ||test == '*' ||test == '/' ||test == '#' ||test == '^'){
            char b = a.charAt(i);
        }
    }
    System.out.println(test);
    System.out.println(test1);
    double number = Double.parseDouble(test1);

    // checks that if the user intends to squareroot the first number
    if (test == '#'){
        // square roots the first number
        System.out.println(java.lang.Math.sqrt(a));
    }
    else{

        //creates a Scanner
        Scanner reader2 = new Scanner(System.in);
        //promts the user for an input
        System.out.println("Please enter Second number");
        String c = reader2.nextLine();
        reader2.close();

        // checks if user wanted to add
        if (test == '+'){
            // adds the two numbers
            System.out.println(a+c);
        }
        //checks if user wanted to subtract
        else if (test == '-'){
            // subtracts the two numbers
            //System.out.println(a-c);
        }
        //checks if user wanted to multiply
        else if (test == '*'){
            // multiplies the two numbers
            //System.out.println(a*c);
        }
        //checks if the user wanted to divide
        else if (test == '/'){
            // devides the two numbers
            //System.out.println(a/c);
        }
        //checks if the user wants to power the numbers
        else if (test == '^'){
            // powers the first number by the second number
            //System.out.println(java.lang.Math.pow(a,c));
        }
        // if the programme is unsure what the user intended then a error message will be printed to screen
        else{
            System.out.println("ERROR.UserInputForFunctionVoid");
        }
    }
}
publicstaticvoidmain(字符串[]args){
//向用户介绍该程序
System.out.println(“欢迎参加我的计算器测试”);
System.out.println(“函数输入;+加数,-减数,*乘法,/除法,^乘幂,#平方根”);
//创建扫描仪
扫描仪读取器0=新扫描仪(System.in);
//提示用户输入
System.out.println(“请输入第一个数字”);
字符串a=reader0.nextLine();
reader0.close();
字符测试='x';
字符串test1=“”;
for(inti=0;i
如果您能帮助解决此问题,我们将不胜感激


谢谢

首先,它不会编译为
Math。sqrt(a)
as
String
不能像这样转换为
double
。即使
a
double
,我也不认为它会像预期的那样工作

你最好先读
int
double
,比如
double a=reader0.nextDouble()

通过读入第一个数字,然后读入运算符,可以将代码缩短得太少。然后检查是否需要平方根(我想在这个阶段你不需要第二个数字),然后要求第二个数字

我也会先声明我的结果(
double result=0.0;
),然后在最后打印出来

因此,在您检查
#
之后,只需将其余部分放入
else

if (operator.equals("+")) {
            result = a + b;
        } else if (operator.equals("-")) {
            result = a - b;
        } 
        //and so on...

<强>注:您的结果可能会在小数点之后出现X个位数,因此可能考虑使用<代码> Strord.Frand(“%.2f”,结果)< /C>或类似的内容。

<强>附加注释:< /强>您也可以考虑使用<代码>开关<代码>代替<代码>如果……如果


这里是一个。

请注意,代码中的注释大部分是不正确的,因为它们来自代码的早期迭代。
-请努力从您发布的代码中删除不相关的注释。
System.out.println(a+c)a和c是字符串,因此
+
运算符不执行数字加法。“仅打印先前输入的方程式”-这不应编译。另见:。