- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在启动 UI 时遇到错误,导致此代码在标题中向我吐出错误。它适用于我的所有其他运算符符号,所以我真的不确定这里发生了什么。我不想发布所有代码,所以如果这还不够,您可以在我的 gitHub 上找到其余代码:https://github.com/jparr721/Calculator-App/tree/master/src/calculator
public class Calculation_Controls {
public double A, B;
private String[] operators = new String[] {"-","+","/","*","x","^","X"};
/**
* Check for the symbol being used within the TextArea to then
* apply the correct caculation method.
* FIXME - Allow for multiple symbols to be used and have them return
* FIXME - a result in accordance with PEMDAS
*
*@param nums
*
* @return operator, or error
*/
public String findSymbol(String nums) {
for (String operator : operators) {
if (nums.contains(operator)) {
return operator;
}
}
return "invalid input";
}
/**
* Input method to take the user input from the text area
* and apply the correct calculation to it
*
* @param nums - Stores the input as a String which I then convert to an int
* then back to a string to be printed to the TextArea
*
* @return - The result of the calculation as a string
*/
public String input(String nums){
String operator = findSymbol(nums);
if (operator == null){
System.out.println("Invalid input");
}
String[] split = nums.split(operator);
int left = Integer.parseInt(split[0]);
int right = Integer.parseInt((split[1]));
String result = "";
switch (operator){
case "+":
result = Double.toString(add(left, right));
break;
case "-":
result = Double.toString(subtract(left, right));
break;
case "*":
case "x":
case "X":
result = Double.toString(multiply(left, right));
break;
case "/":
result = Double.toString(divide(left, right));
break;
case "^":
result = Double.toString(pwr(left, right));
break;
default:
System.out.println("Invalid Operator");
}
return result;
}
最佳答案
正则表达式中有保留字符,您应该对这些字符进行转义以实现您想要的效果。例如,你不能使用String.split("+")
,你必须使用String.split("\\+")
。
正确的运算符是:
String[] operators = new String[] {"-","\\+","/","\\*","x","\\^","X"};
关于java.util.regex.PatternSyntaxException : Dangling meta character '+' near index 0 +,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40246231/
你认为这是一个悬空else的情况吗? 根据编译器和印度理工学院教授的说法,确实如此。但我有疑问!从理论上讲,在满足 if 的条件后,它总是只会进一步处理一条语句(或者一个复合语句,即括号内的多个语句)
我正在使用 dangle.js,这是一个基于 D3 为 Angular JS 构建的可视化工具。我正在尝试通过使用 html 调用指令来输出图形 在我的 Controller 中我有代码 $scop
给定以下有效代码: Boolean a = false; if (a) System.out.println("A"); else System.out.println("!A"); 根据
我想用 jQuery 或更好的纯 javascript 创建一个动画,使 div “悬垂”。我附上了显示动画的动画 gif。我不知道如何重新创建它,如果它是我可以使用现有的 jquery 缓动/动画或
我正在为 JavaFX 创建一个带有登录名和用户名的识别窗口,我正在使用正则表达式来确保密码至少包含一个特殊字符和一位数字。 这是我正在使用的正则表达式 newValue.matches("*\\d+
Git的 fsck doc谈论“悬空”对象,而 the gc doc只谈论“松散的物体”。有严格的划分。 但是在浏览一些相关的 SO 帖子时,这些术语似乎可以互换使用。在 Git Book v2还有
我经常看到悬空的 else 处理为: if (x > 0) if (y > 0) print "hello" else print "world" 解析器或解释器实际上会将 else
考虑以下问题:我有许多类,每个类都实现一个 get() 函数。以下 container1 和 container2 是此类的示例: struct expensive_type { int v;
我想构建一个模式来将逗号插入整数以使其看起来不错,例如 1234556 ,操作后,就会像 1,234,556 ,所以我使用以下代码: public static void insertCommaFor
在 Javascript 中,如果您引用了某个 DOM 元素,然后通过向 document.body 添加其他元素来修改 DOM,您的 DOM 引用就会失效。 考虑以下代码: function
这个问题在这里已经有了答案: Can a local variable's memory be accessed outside its scope? (20 个答案) 关闭 7 年前。 这可能是一
由于我更新了我的 Android Studio (2.3.1) 和构建工具 (2.3.1),我收到了警告, Warning: Dangling Javadoc comment 对于像这样的评论, /*
在 Scala 中,可以在函数中定义局部 block 。本地 block 计算最后的语句,例如, val x = {val x =1;x+1} 此处 x==2,内部 val x 是该 block 的本
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 3 年前。 Improve this ques
我希望我的 node.js 测试能够确保,一旦测试结束并且 test.finish() 或类似的调用被调用,就没有更多的代码等待运行。没有更多的 I/O 等待完成,没有更多的定时器等待触发,等等。 这
我知道有些人认为前导下划线的存在暗示变量是“私有(private)的”,这种隐私是虚构的,并假设这就是为什么 JSLint 报告带有错误消息的此类名称。 我在我正在构建的网站上使用 Google An
我正在开发一个 Cocoa-Touch 应用程序,它使用 CoreData 并具有一些由 XCode 模型编辑器生成的 NSPersistentObject 子类。 我注意到最近,在保存上下文时,我收
我在使用 Core Data 时遇到了一个非常令人困惑的问题。在我的核心数据存储中,对于现有的核心数据对象,我检查是否存在关系,如果不存在,我将像这样创建对象(这是 AFFingerprintGene
在另一个question ,我发现Assigned()功能与 Pointer <> nil 相同。我一直以来的理解是Assigned()正在检测这些悬空指针,但现在我知道它没有。悬空指针是那些可能在某
我在 iOS 上遇到以下崩溃: Unresolved error Error Domain=NSCocoaErrorDomain Code=1550 "The operation couldn
我是一名优秀的程序员,十分优秀!