返回
多御红包活动

loops-JAVA——开发一个名为calculator的计算器程序,它输入一个算术表达式,并从字符串计算表达式的结果

发布时间:2022-09-10 15:37:12 108
# java# java# 脚本# 信息

开发一个名为calculator的计算器程序,输入一个“calculator”形式的算术表达式;数字操作员编号=“;并计算表达式的结果。

表达式将从左到右求值,不考虑正则运算符优先级。例如,表达式“quot;14-5*3=";将产生27.0。

“价值”=&引用;显示最终结果并终止程序。

我真的不知道从哪里开始。我不允许使用数组或脚本引擎,因为我们还没有在课堂上学习。以下是我到目前为止得到的信息:

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        //Create Scanner
        Scanner scnr = new Scanner(System.in);
        
        //Initialize Variables
        double number;
        String input;
        final String END = "=";
        
        //User Input
        System.out.println("Enter your numeric expression in the following form: \r\n" + "\"number operator number operator number = \"\r\n" + "Leave a blank space after each number or operator.\r\n" + "Example: 3.5 * 3 - 5 / 2.5 =");
        input = scnr.next();
        
        //Start of loop
        while (!input.equals(END)) {
            number = Double.parseDouble(input);
            
                switch(input) {
                    case A NUMBER:                      
                        break;
                    case +:                     
                        break;
                    case -:                     
                        break;
                    case /:                     
                        break;
                    case *:                     
                        break;

                }
            
            
            }

    }

}

我想这个解决方案与switch语句有关,但我不完全确定。请注意,我并不是在寻找非常复杂或完整的答案,因为我第一节代码课才上了一半。

编辑我只能用两个数字,怎么才能用三个或更多的数字呢?以下是我目前掌握的情况:

import java.util.Scanner;

public class Calculator {

    
    //Create isNumeric method
     public static boolean isNumeric(String str) {
            return str != null && str.matches("[-+]?\\d*\\.?\\d+");
        }
    
    // Compute an arithmetic expression 
    public static void main(String[] args) {
        // Declare the identifiers
        final String END = "=";
        String input;
        double number = 0;
        double number2 = 0;
        char operator = 0;
        double answer = 0;
        
        //make a scanner
        Scanner scnr = new Scanner (System.in);
        
        //Display instructions
        System.out.println("Enter your numeric expression in the following form: \r\n" + "\"number operator number operator number = \"\r\n" + "Leave a blank space after each number or operator.\r\n" + "Example: 3.5 * 3 - 5 / 2.5 =");

        // Input
        System.out.print("> ");
        input = scnr.next();
        
        // Process the first item and input and process the rest of the items 
        while (!input.equals(END)){
            
            switch (input){
                case "+":
                    operator = '+';
                    System.out.println(operator);

                    break;
                case "-":               
                    operator = '-';
                    System.out.println(operator);

                    break;
                case "*": 
                    operator = '*';
                    System.out.println(operator);

                    break;
                case "/": 
                    operator = '/';
                    System.out.println(operator);

                    break;
                default:
                    if (operator == 0) {
                    number = Double.parseDouble(input);
                    System.out.println("First Number is " + number);
                    }
                    else if (isNumeric(input) == true) {
                        number2 = Double.parseDouble(input);
                        System.out.println("Second number is: " + number2);
                            }
                    } // end of switch
                    
            } // end of while-loop
        
        input = scnr.next();            
        
        switch (operator){
        case '+':
            answer =  number + number2;
            break;
        case '-':               
            answer =  number - number2;
            break;
        case '*': 
            answer =  number * number2;
            break;
        case '/': 
            answer =  number / number2;
            break;
        }
        // Display the answer
        System.out.println("> " + answer);
        System.out.println("Thank you!");

        
        

    }

}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像