转载

java – RPNCalculator代码混淆

这是我的第二个编程类,我是Java新手.我一直在做我的第一个任务,它涉及类和方法.我对这些话题知之甚少,发现自己迷失了.我的任务要求我创建一个RPN计算器,询问用户两个数字和一个运算符.计算器对这两个数字执行操作,并要求再增加一个数字和一个运算符.然后,计算器使用第一组的结果,并使用刚输入的新数字执行另一个操作.当用户输入一个字母时,程序结束.我附上我的代码(原始).我需要尽可能多的帮助理解方法,就像编码一样.请尽快帮助,我想学习这个.任何帮助表示赞赏.
import java.util.Scanner;

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

        double v1, v2;
        String operator = keyboard.nextLine();
        char symbol = operator.charAt(0);

        System.out.print("Enter a value v1: ");
        v1 = keyboard.nextDouble();
        System.out.println();
        System.out.print("Enter a value v2: ");
        v2 = keyboard.nextDouble();
        System.out.println();
        System.out.print("Enter one of the valid operators +, -, *, /, nCr,:  ");
        operator = keyboard.nextLine();
        switch (symbol)
            {
             case'+':
                sum.writeOutput();
                break;
             case'-':
                minus.writeOutput();
                break;
             case'*':
                times.writeOutput();
                break;
             case'/':
                divide.writeOutput();
                break;
             case'q':
                System.out.println("Your last result was "  );
             default:
                System.out.println("You must choose an appropriate operator .");
             }
    }

    double value1, value2;
    int n;

    public static double sum(double value1,double value2)
    {
        double newSum = value1 + value2;
        return newSum;
    }
    public static double minus(double value1, double value2)
    {
        double newMinus = value1 - value2;
        return newMinus;
    }
    public static double times(double value1, double value2)
    {
        double newTimes = value1 * value2;
        return newTimes;
    }
    public static double divide(double value1, double value2)
    {
        double newDivide = value1 / value2;
        return newDivide;
    }
}

好.开始了.我已经修改了你的程序了一点点.以下是修改程序时出现的问题.

1.任何类中都没有“writeOutput()方法.因此我不得不删除那段代码.

2.必须引入while循环才能使其成为迭代过程.

3.在读取运算符字符串后向下移动operator.charAt(0)方法.

由于您使用的是switch语句,因此无法将运算符值与“nCr”字符串值进行比较.要使用此方法,您必须使用equals()方法使用字符串比较.

最后但并非最不重要的是,操作的结果应该是下一轮计算的输入之一.意味着第一个操作的结果将被传递到下一个操作的第一个参数.

import java.util.Scanner;

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

        double v1, v2;
//        String operator = keyboard.nextLine();
//        char symbol = operator.charAt(0);
        char operator = ' ';//First time around, set this to an something other than 'q'
        String operatorString = " ";
        System.out.print("Enter a value v1: ");
        v1 = keyboard.nextDouble();
        System.out.println();
        while(operator != 'q')
        {
            System.out.print("Enter a value v2: ");
            v2 = keyboard.nextDouble();
            System.out.println();
            System.out.print("Enter one of the valid operators +, -, *, /, nCr,:  ");
            operatorString = keyboard.next();//nextLine() doesn't wait until the user hit enter
            operator = operatorString.charAt(0);
            switch (operator)
            {
             case'+':
                v1 = sum(v1, v2);
                System.out.println(v1);
                break;
             case'-':
                v1 = minus(v1, v2);
                System.out.println(v1);
                break;
             case'*':
                v1 = times(v1, v2);
                System.out.println(v1);
                break;
             case'/':
                v1 = divide(v1, v2);
                System.out.println(v1);
                break;
             case'q':
                System.out.println("Your last result was "  );                
             default:
                System.out.println("You must choose an appropriate operator .");
             }
        }
    }

    double value1, value2;
    int n;

    public static double sum(double value1,double value2)
    {
        double newSum = value1 + value2;
        return newSum;
    }
    public static double minus(double value1, double value2)
    {
        double newMinus = value1 - value2;
        return newMinus;
    }
    public static double times(double value1, double value2)
    {
        double newTimes = value1 * value2;
        return newTimes;
    }

    public static double divide(double value1, double value2)
    {
        if (value2 == 0)
        {
            System.out.println("Division by Zero. Try again");
            return value1;
        }
        double newDivide = value1 / value2;
        return newDivide;
    }
}

翻译自:https://stackoverflow.com/questions/16868655/rpncalculator-code-confusion

原文  https://codeday.me/bug/20190110/504872.html
正文到此结束
Loading...