java – 使用||压缩基本代码带字符串的运算符
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用||压缩基本代码带字符串的运算符,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3753字,纯文字阅读大概需要6分钟。
内容图文
![java – 使用||压缩基本代码带字符串的运算符](/upload/InfoBanner/zyjiaocheng/759/396a1caab6ab47ba8c378f20fa50b266.jpg)
我是java编程的新手.我无法找到有关使用||的任何信息运算符与字符串.我想知道是否有更有效的方法来执行此代码,特别是仍然易于阅读.我尝试使用一个简单的计算器来熟悉IfThenElse语句.
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
double first;
double second;
String option;
while(true){
System.out.println("What function would you like to calculate?");
option=input.next();
if(option.equals("add") || option.equals("+")){
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double add=first+second;
System.out.println(add);
}
else if(option.equals("subtract") || option.equals("-")) {
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double subtract=first-second;
System.out.println(subtract);
}
else if(option.equals("multiply") ||option.equals("*")) {
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double multiply=first*second;
System.out.println(multiply);
}
else if(option.equals("divide") || option.equals("/")) {
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double divide=first/second;
System.out.println(divide);
}
else if(option.equals("end")){
System.exit(0);
}
}
}
}
在大多数情况下,我想知道如果要求,我已经测试过它们确实有效,但对我来说似乎有点笨拙.但是,任何批评都会受到高度赞赏.
解决方法:
switch / case语句是一系列ifs的不错选择,而as of Java 7可以使用带有字符串的switch语句.注意两者之间的语法差异.每个案例都以break语句结尾,而不是用花括号分组.
switch (option) {
case "add":
case "+":
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double add=first+second;
System.out.println(add);
break;
case "subtract":
case "-":
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double subtract=first-second;
System.out.println(subtract);
break;
case "multiply":
case "*":
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double multiply=first*second;
System.out.println(multiply);
break;
case "divide":
case "/":
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double divide=first/second;
System.out.println(divide);
break;
case "end":
System.exit(0);
}
然后我会建议组合重复的提示代码.如果您发现自己正在复制和粘贴代码,那么退后一步并找出如何避免重复通常是一个好主意.重复的代码表明您应该进行一些重构.
if (option.equals("end")) {
System.exit(0);
}
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
switch (option) {
case "add":
case "+":
double add=first+second;
System.out.println(add);
break;
case "subtract":
case "-":
double subtract=first-second;
System.out.println(subtract);
break;
case "multiply":
case "*":
double multiply=first*second;
System.out.println(multiply);
break;
case "divide":
case "/":
double divide=first/second;
System.out.println(divide);
break;
}
此外,您还可以通过对所有计算使用单个结果变量来消除重复的打印输出.
if (option.equals("end")) {
System.exit(0);
}
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double result;
switch (option) {
case "add": case "+": result = first + second; break;
case "subtract": case "-": result = first - second; break;
case "multiply": case "*": result = first * second; break;
case "divide": case "/": result = first / second; break;
}
System.out.println(result);
内容总结
以上是互联网集市为您收集整理的java – 使用||压缩基本代码带字符串的运算符全部内容,希望文章能够帮你解决java – 使用||压缩基本代码带字符串的运算符所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。