一、算数操作符类别

  • 基本的有:

+

-

*

/

%

  1. 自增 自减:

++

--

二、基本算数操作符

+

-

*

/

基本的加 减 乘 除

public class HelloWorld {

public static
void main(String[] args) {

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
void main(String[] args) {

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
void main(String[] args) {

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
void main(String[] args) {

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
void main(String[] args) {

int i = 5;

int j = 2;

System.out.println(i%j); //输出为1

}

}

七、自增 、自减

++

--

在原来的基础上增加1或者减少1

public class HelloWorld {

public static
void main(String[] args) {

int i = 5;

i++;

System.out.println(i);//输出为6

}

}

八、自增、自减操作符置前以及置后的区别

以++为例

int i = 5;

i++; 先取值,再运算

++i; 先运算,再取值

public class HelloWorld {

public static
void main(String[] args) {

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
void main(String[] args) {

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) = ?

参考: 使用Scanner读取浮点数的办法

要求的实现效果:

可以根据BMI指数表增加判断肥胖及健康情况的功能

官方答案:

import java.util.Scanner;

public class HelloWorld {

public static
void main(String[] args) {

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
void main(String[] args) {

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
float getBMI(Object object){//方法必须是静态的,才能在主方法main中调用。

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;

}

}

最新文章

  1. poj3122-Pie(二分法+贪心思想)
  2. 将jquery.shCircleLoader插件修改为zepto.js兼容
  3. Hyhyhy – 专业的 HTML5 演示文稿工具
  4. python os模块(1)
  5. 【转载】Solr4+IKAnalyzer的安装配置
  6. .net重启iis线程池和iis站点程序代码分享
  7. 未指定的错误,发生了一个 Oracle 错误,但无法从 Oracle 中检索错误信息。数据类型不被支持。
  8. [原]ubuntu下制作ubuntu源
  9. 必须声明标量变量 &quot;@列名&quot;
  10. LD1-M(简单图的判定+构造,Havel定理)
  11. ACM POJ 2192 Zipper
  12. C++ 类的继承、虚拟继承、隐藏、占用空间
  13. C#导入导出Excele数据
  14. 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
  15. nodejs,javascript过滤emoj表情
  16. python3 json.dump乱码问题
  17. Jest &amp; React &amp; Enzyme
  18. BZOJ 4805: 欧拉函数求和 杜教筛
  19. 堆优化的dij【模板】
  20. Gulp API之怎样压缩CSS

热门文章

  1. Spring_mybatis结合之1.1
  2. Vuex 注入 Vue 生命周期的过程
  3. 程序员软件开发最好的IDE集成工具eclipse各个版本的详细介绍。详细介绍,送给初学者的朋友
  4. UI 科学
  5. Lua 5.3注册C++类相关API
  6. 【HttpRunner v3.x】笔记 ——4. 测试用例-结构解析
  7. rpc中的高并发
  8. 面试【JAVA基础】类加载机制
  9. &lt;init&gt;与&lt;clinit&gt;,static与final与static final
  10. 想写一篇jvm的工具入门