首页 / JAVA / java – 使用字符运算符执行操作
java – 使用字符运算符执行操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用字符运算符执行操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1654字,纯文字阅读大概需要3分钟。
内容图文
![java – 使用字符运算符执行操作](/upload/InfoBanner/zyjiaocheng/764/73061630de994972af631abf5ef1695b.jpg)
我正在解决我正在研究的计算器程序的问题.我遇到的问题是我不知道如何让操作起作用.我已经尝试将它连接成一个String,然后以这种方式执行操作,但你不能这样做.我也尝试在操作数之间使用操作数和运算符(char b),但它不会执行操作.我现在唯一的解决方案是执行一系列检查以确定运算符是什么,然后使用该运算符执行等式,例如if(b ==’ – ‘){int answer = xy;} .我这样做的唯一问题是它感觉草率,并且可以以更有效的方式完成.
/**
* Makes sure that char b is a binary operator and returns the value made from x b y
*
* @param x first operand of integer value
* @param b the operation value
* @param y second operand of integer value
* @return the operation of x b y where b is the binary operator, +,-,/,*,%
*/
public int binaryOperation(int x, char b, int y)
{
if (!(b == '+' || b == '-' || b == '/' || b == '*' || b == '%'))
{
System.out.println("The character provided is not a valid binary operator. Please use one of the following characters:"
+ " '+', '-', '/', '*', or '%'.");
}
else
{
int answer =
return answer;
}
}
/**
* Makes sure that b is an unary operator and returns the value made from x b y
*
* @param x first operand of with an integer value
* @param b the operation value
* @param y second operand
* @return the operation of x b y where b is the unary operator, + or -
*/
public int unaryOperation(int x, char b, int y)
{
if (!(b == '+' || b == '-'))
{
System.out.println("The character provided is not a valid unary operator. Please use one of the following characters:"
+ " '+' or '-'.");
}
else
{
String function = System.out.println(x + b + y);
int answer = (int) function;
return answer;
}
}
解决方法:
if (b == '+'){
return x+y;
}
else if (b == '-') {
return x-y;
}
等等…
内容总结
以上是互联网集市为您收集整理的java – 使用字符运算符执行操作全部内容,希望文章能够帮你解决java – 使用字符运算符执行操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。