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