import java.util.Scanner;
public class Rpn_calculator
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double ans = 0;
double n = 0;
double r = 0;
String j;
System.out.println();
System.out.println();
System.out.print("Please enter a value for n ");
n = keyboard.nextDouble();
System.out.print("Please enter a value for r ");
r = keyboard.nextDouble();
System.out.println("Please choose one of these operands:+,-,*,nCr,/ to continue, or q to quit ");
j = keyboard.nextLine();
while (j != "q")
{
if(j == "+")
ans = n + r;
if(j == "-")
ans = n - r;
if(j == "*")
ans = n * r;
if(j == "/")
ans = n / r;
if(j == "nCr")
ans = factorial(n + r -1) / (factorial(r)*factorial(n - 1));
System.out.print(ans);
System.out.print("Please enter a value for n ");
n = keyboard.nextDouble();
System.out.print("Please enter a value for r ");
r = keyboard.nextDouble();
System.out.print("Please choose one of these operands:+,-,*,nCr,/ to continue or q to quit ");
j = keyboard.nextLine();
}
}
public static double factorial(double x);
{
double sum = 1;
int i;
for (i = 0; i<= x; i++);
sum = sum * i;
return sum;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试为这个任务创建一个RPN计算器,我相信我有我需要的功能,但我的因子函数有语法错误.它说"缺少方法体".
另外,我不明白为什么,当我编译它时,我的字符串输入请求被完全忽略 - 将程序锁定在while循环中.我希望我不会错过一些非常明显的东西.
有几个分号,不应该有:
public static double factorial(double x); // <-- This one
{
double sum = 1;
int i;
for (i = 0; i<= x; i++); // <-- This one
sum = sum * i;
return sum;
}
Run Code Online (Sandbox Code Playgroud)
在签名后立即放置分号时,您没有方法体,因此出现错误.该for循环不会是一个编译器错误,但是这将是一个空循环.
另外,我认为你需要用来String.equals比较Java中的字符串,而不是==运算符.