- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近开始在大学学习java,我们要做的任务是理解递归并向这个波兰表示法代码添加阶乘函数。我尝试过各种方法,这是最新的:
public class PolishNotation {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Please enter the operators");
System.out.println("for operators +, -, *, and !");
System.out.println("Leave spaces between all operators and digits");
System.out.print("expression: ");
System.out.println("value = " + evaluateEXP(scanner));
}
}
//input contains the expression user has entered
public static int evaluateEXP(Scanner scanner) {
//checks if there is another digit after current one
//allows expression to be looked at from right to left
if (scanner.hasNextInt())
return scanner.nextInt();
//if there is another digit after current one then
//operands and operators are established
char operator = scanner.next().charAt(0);
int operand1 = evaluateEXP(scanner);
int operand2 = evaluateEXP(scanner);
return evaluateOP(operator, operand1, operand2);
}
//operator has to be one of +, - , * or ! otherwise error is given
private static int evaluateOP(char operator, int operand1, int operand2) {
if (operator == '+')
return operand1 + operand2;
if (operator == '-')
return operand1 - operand2;
if (operator == '*')
return operand1 * operand2;
if (operator == '/')
return operand1 / operand2;
if (operator == '!')
//if ! used then uses factorial method
return factorial(operand1);
//RunTimeException allows to return an error string in a int "type" method
throw new RuntimeException("operator not allowed for this language");
}
private static int factorial(int n) {
return n == 1 ? 1 : factorial(n - 1) * n;
}
}
没有错误,但结果没有出来,所以我猜测它陷入了无限循环。这段代码的想法是,如果我这样做了! + 3 2 它应该做 !5 所以返回 120 并且我不能使用 while 或 for 循环。其余操作数都有效,只是阶乘不起作用。
最佳答案
问题在于,在 evaluateEXP
中,您的代码始终需要 2 个操作数。然而 !
仅接受一个操作数,因此如果您输入类似 ! 5
它将等待更多输入。
解决方案是检查运算符是一元还是二元,如果是一元则只接受一个操作数。以下是重构代码以实现此目的的几种方法:
1) 检查 evaluateEXP
方法中的运算符,仅获取第二个操作数(如果它是二进制的)(在您的情况下不是 !
):
//input contains the expression user has entered
public static int evaluateEXP(Scanner scanner) {
//checks if there is another digit after current one
//allows expression to be looked at from right to left
if (scanner.hasNextInt())
return scanner.nextInt();
//if there is another digit after current one then
//operands and operators are established
char operator = scanner.next().charAt(0);
int operand1 = evaluateEXP(scanner);
int operand2 = 0;
//only take second operand if operator is not unary
if (operator != '!') {
operand2 = evaluateEXP(scanner);
}
return evaluateOP(operator, operand1, operand2);
}
2)将扫描器传递给evaluateOP
并让它直接获取操作数:
//input contains the expression user has entered
public static int evaluateEXP(Scanner scanner) {
//checks if there is another digit after current one
//allows expression to be looked at from right to left
if (scanner.hasNextInt())
return scanner.nextInt();
//if there is another digit after current one then
//operands and operators are established
char operator = scanner.next().charAt(0);
return evaluateOP(operator, scanner);
}
//operator has to be one of +, - , * or ! otherwise error is given
private static int evaluateOP(char operator, Scanner scanner) {
if (operator == '+')
return evaluateEXP(scanner) + evaluateEXP(scanner);
if (operator == '-')
return evaluateEXP(scanner) - evaluateEXP(scanner);
if (operator == '*')
return evaluateEXP(scanner) * evaluateEXP(scanner);
if (operator == '/')
return evaluateEXP(scanner) / evaluateEXP(scanner);
if (operator == '!')
//if ! used then uses factorial method
return factorial(evaluateEXP(scanner));
//RunTimeException allows to return an error string in a int "type" method
throw new RuntimeException("operator not allowed for this language");
}
3)在第二个解决方案的基础上,您还可以合并这两种方法,因为它们无论如何都紧密相连:
//input contains the expression user has entered
public static int evaluateEXP(Scanner scanner) {
//checks if there is another digit after current one
//allows expression to be looked at from right to left
if (scanner.hasNextInt())
return scanner.nextInt();
//if there is another digit after current one then
//operands and operators are established
char operator = scanner.next().charAt(0);
if (operator == '+')
return evaluateEXP(scanner) + evaluateEXP(scanner);
if (operator == '-')
return evaluateEXP(scanner) - evaluateEXP(scanner);
if (operator == '*')
return evaluateEXP(scanner) * evaluateEXP(scanner);
if (operator == '/')
return evaluateEXP(scanner) / evaluateEXP(scanner);
if (operator == '!')
//if ! used then uses factorial method
return factorial(evaluateEXP(scanner));
//RunTimeException allows to return an error string in a int "type" method
throw new RuntimeException("operator not allowed for this language");
}
关于java - Java 递归前缀解析器阶乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52767633/
发布以下查询时,出现错误响应 {"error":{"root_cause":[{"type":"parsing_exception","reason":"[prefix] query does not
我对 Python 和 Django 真的很陌生......我想做的是: 在 Mac OS 10.6.8 上安装 Python 2.7 安装 pip 安装 Django 安装 virtualenvwr
前缀表达式 前缀表达式又称波兰式,前缀表达式的运算符位于操作数之前。 例如: ( 3 + 4 ) × 5 − 6 (3+4)×5-6(3+4)×5−6 对应的前缀表达式就是 - × + 3 4 5 6
如何在Intel C编译器中定义俄语字符串? 在MSVS 2008中,我这样做: _wsetlocale(LC_ALL, L"Russian"); wprintf(L"текст"); 而且有效。 在
这是我到目前为止所得到的: SPECS = $(shell find spec -iname "*_spec.js") spec: @NODE_ENV=test \ @NODE_PAT
我看到了下面的前缀::它代表什么? :abc 是一个关键字,但是 ::abc 是什么? 谢谢,穆尔塔扎 最佳答案 假设当前命名空间是my.app。然后, ::x 是 :my.app/x 的阅读器简写,
我为我的 discord 创建了一个建议功能,用户可以说 +suggest(建议),它会自动发布到另一个 channel 。 有些事情我需要帮助: 将“建议由用户制作”放入标题中,而不是在单独的行中。
#include int main() { int a=1; printf("%d",(++a)++); return 0; } 此代码出现错误 error: invalid lvalue in
我在使用前缀和后缀运算符对数字执行减法时遇到了一个小问题。这是我的程序: public class postfixprefix { public static void main (Strin
当我在 Android native 浏览器中运行 HTML5 兼容性测试时,它会看到 IndexedDB 支持标记为“Prefixed”,而在 Chrome 和其他浏览器中则标记为“Yes”。我知道
我试过重载运算符--前缀,但我有错误,有人帮忙吗? #include #include "Circulo.h" using namespace std; int main() { //par
我正在尝试在我正在制作的这个论坛上创建一个引用功能,当我按下引用时,我只需用 Markdown 填充 textarea ,但唯一的事情是我需要在每行的 markdown 前面加上 > 前缀,这样它就是
friend 之间打赌。sum 变量定义为全局变量。我们有 2 个线程在循环 1..100 上运行并在每个循环中将 sum 递增 1。 打印什么?“和=”? int sum = 0; void fun
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Post Increment and Pre Increment concept? 谁能明确解释前缀增量与后
从模板类继承时,我需要在派生类中访问的所有基类成员前面加上this: template struct X{ int foo; void bar(); }; template struct
据我所知,在 C++ 中,在同一类的函数成员中调用另一个成员函数不需要“this”前缀,因为它是隐式的。但是,在使用函数指针的特定情况下,编译器需要它。仅当我通过 func 指针为调用包含“this”
例如,考虑以下名称冲突的地方 nest1 : template class nest1 {}; class cls { public: template class nest1 {};
我无法理解下面一段特定代码的逻辑。 int i[] = { 21, 4, -17, 45 }; int* i_ptr = i; std::cout << (*i_ptr)++ << std::endl
有人能给我指出正确的方向吗,我目前有一个可搜索的数据库,但遇到了按标题搜索的问题。 如果标题以“The”开头,那么显然标题将位于“T”部分,避免搜索“The”的好方法是什么?我应该连接两个字段来显示标
我在 2 小时前创建了一个新项目。以与我的旧(不同)项目相同的方式配置它,一切正常。 在我的 podfile 中我有: pod 'CocoaLumberjack', '2.0.0-rc2' 如果我在
我是一名优秀的程序员,十分优秀!