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;
}
}
我正在尝试为此作业创建一个RPN计算器,我相信我有我需要的功能,但我的因子函数有语法错误。它说“缺少方法体”。
另外,我不明白为什么,当我编译它时,我的字符串输入请求被完全忽略 - 将程序锁定在while
循环中。我希望我不会错过一些非常明显的东西。
答案 0 :(得分:6)
有几个分号不应该是:
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;
}
当您在签名后立即放置分号时,您没有方法体,因此出现错误。 for
循环不是编译器错误,但它将是一个空循环。
另外,我认为您需要使用String.equals
来比较Java中的字符串,而不是==
运算符。