Java知识系统回顾整理01基础04操作符01算术操作符
一、算数操作符类别
- 基本的有:
+ - * / % |
- 自增 自减:
++ -- |
二、基本算数操作符
+ - * / |
基本的加 减 乘 除
public class HelloWorld { public static int i = 10; int j = 5; int a = i+j; int b = i - j; int c = i*j; int d = i /j; } } |
三、练习--求和
题目:
使用Scanner从控制台获取两个数字,然后计算这两个数字的和,达到下图中的效果。
如果不会使用Scanner,请参考 如何使用Scanner读取整数
官方答案:
import java.util.Scanner; public class HelloWorld { public static Scanner s = new Scanner(System.in); int a = s.nextInt(); System.out.println("第一个整数:"+a); int b = s.nextInt(); System.out.println("第二个整数:"+b); int c = a+b; System.out.println("两个数的和是 :" + c); } } |
个人整理答案:
四、任意运算单元的长度超过int
如果有任何运算单元的长度超过int,那么运算结果就按照最长的长度计算
比如:
int a = 5;
long b = 6;
a+b -> 结果类型是long
public class HelloWorld { public static int a = 5; long b = 6; int c = (int) (a+b); //a+b的运算结果是long型,所以要进行强制转换 long d = a+b; } } |
五、任意运算单元的长度小于int
如果任何运算单元的长度都不超过int,那么运算结果就按照int来计算
比如:
byte a = 1;
byte b= 2;
a+b -> int 类型
public class HelloWorld { public static byte a = 1; byte b= 2; byte c = (byte) (a+b); //虽然a b都是byte类型,但是运算结果是int类型,需要进行强制转换 int d = a+b; } } |
六、%取模
% 取余数,又叫取模
比如:5除以2,余1
public class HelloWorld { public static int i = 5; int j = 2; System.out.println(i%j); //输出为1 } } |
七、自增 、自减
++
--
在原来的基础上增加1或者减少1
public class HelloWorld { public static int i = 5; i++; System.out.println(i);//输出为6 } } |
八、自增、自减操作符置前以及置后的区别
以++为例
int i = 5;
i++; 先取值,再运算
++i; 先运算,再取值
public class HelloWorld { public static int i = 5; System.out.println(i++); //输出5 System.out.println(i); //输出6 int j = 5; System.out.println(++j); //输出6 System.out.println(j); //输出6 } } |
九、练习--自增
题目:
int i = 1;
int j = ++i + i++ + ++i + ++i + i++;
问 j的结果是多少?
注: 先不要放在eclipse中,根据++置前 置后的理解自己先算一遍,然后再看答案
官方答案:
public class HelloWorld { public static int i = 1; int j = ++i + i++ + ++i + ++i + i++; //i值 2 3 4 5 6 //取值 2 2 4 5 5 System.out.println(j); } } |
个人整理答案:
心算给出答案18
十、练习--BMI
题目:
使用Scanner收集你的身高体重,并计算出你的BMI值是多少
BMI的计算公式是:体重(kg) / (身高*身高)
比如邱阳波的体重是72kg, 身高是1.69,那么这位同学的BMI就是
72 / (1.69*1.69) = ?
要求的实现效果:
可以根据BMI指数表增加判断肥胖及健康情况的功能
官方答案:
import java.util.Scanner; public class HelloWorld { public static Scanner s = new Scanner(System.in); System.out.println("请输入身高(m):"); float height = s.nextFloat(); System.out.println("请输入体重(kg):"); float weight = s.nextFloat(); float BMI = weight/ (height*height); System.out.println("当前的BMI是: " + BMI); } } |
个人整理答案:
public class Operator01A { public static Operator01 person = new Operator01(); float urBMI = getBMI(person); if (urBMI<18.5) { System.out.println("您的BMI指数为:"+ urBMI+",您的体重过轻"); }else if (urBMI < 24) { System.out.println("您的BMI指数为:"+ urBMI+",您的体重处于正常范围"); }else if (urBMI < 27) { System.out.println("您的BMI指数为:"+ urBMI+",您的体重超重"); }else if (urBMI < 30) { System.out.println("您的BMI指数为:"+ urBMI+",您处于轻度肥胖状态"); }else if (urBMI < 35) { System.out.println("您的BMI指数为:"+ urBMI+",您处于中度肥胖状态"); }else { System.out.println("您的BMI指数为:"+ urBMI+",您重度肥胖,急需减肥"); } } public static Scanner scanner = new Scanner(System.in); System.out.println("请输入您的身高(m):"); float bodyHeight = scanner.nextFloat(); System.out.println("请输入您的体重(kg):"); float bodyWeight = scanner.nextFloat(); float BMI = bodyWeight/(bodyHeight*bodyHeight); return BMI; } } |
最新文章
- poj3122-Pie(二分法+贪心思想)
- 将jquery.shCircleLoader插件修改为zepto.js兼容
- Hyhyhy – 专业的 HTML5 演示文稿工具
- python os模块(1)
- 【转载】Solr4+IKAnalyzer的安装配置
- .net重启iis线程池和iis站点程序代码分享
- 未指定的错误,发生了一个 Oracle 错误,但无法从 Oracle 中检索错误信息。数据类型不被支持。
- [原]ubuntu下制作ubuntu源
- 必须声明标量变量 ";@列名";
- LD1-M(简单图的判定+构造,Havel定理)
- ACM POJ 2192 Zipper
- C++ 类的继承、虚拟继承、隐藏、占用空间
- C#导入导出Excele数据
- SpringBoot+mybatis:报错Fri Oct 19 14:29:24 CST 2018 WARN: Establishing SSL connection without server&#39;s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requiremen
- nodejs,javascript过滤emoj表情
- python3 json.dump乱码问题
- Jest &; React &; Enzyme
- BZOJ 4805: 欧拉函数求和 杜教筛
- 堆优化的dij【模板】
- Gulp API之怎样压缩CSS
热门文章
- Spring_mybatis结合之1.1
- Vuex 注入 Vue 生命周期的过程
- 程序员软件开发最好的IDE集成工具eclipse各个版本的详细介绍。详细介绍,送给初学者的朋友
- UI 科学
- Lua 5.3注册C++类相关API
- 【HttpRunner v3.x】笔记 ——4. 测试用例-结构解析
- rpc中的高并发
- 面试【JAVA基础】类加载机制
- <;init>;与<;clinit>;,static与final与static final
- 想写一篇jvm的工具入门