3、javaSE(运算符&分支&循环)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了3、javaSE(运算符&分支&循环),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含14267字,纯文字阅读大概需要21分钟。
内容图文
![3、javaSE(运算符&分支&循环)](/upload/InfoBanner/zyjiaocheng/619/be54f7d2284a4961b66548b7db5c5ff3.jpg)
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一、运算符
1.算数运算符
**+ - * / ++ -- %(取余)**
代码如下(示例):
public class Demo03 {
//算数运算符
public static void main(String[] args) {
//运算符左右两边类型必须一致
double a = 1 + 3.14;
//整除问题
int b = 10 / 3;
System.out.println(b);//3
double d = 5 / 2;
System.out.println(d);//2.0
double d2 = 5.0 / 2;//自动转换
System.out.println(d2);//2.5
System.out.println(b++);//3 先使用后自加
System.out.println(b);//4
System.out.println(++b);//5 先自加后使用
System.out.println(b);//5
}
}
2.赋值运算符
**= += -= *= /=**
3.比较运算符
**> < >= <= != ==
结果返回boolean类型**
4.逻辑运算符
***&& || ^ ! & |
符号左右两边都是boolean表达式
结果返回也是boolean表达式
&& &(一假即假): 两个都是true的时候才是true
|| |(一真即真): 只要有一个是true的时候就是true
&& || -> 短路操作
短路操作的特点:
一旦出现结果,就不在继续后续操作***
代码如下(示例):
/*
* 逻辑运算符
*/
public class Demo05 {
public static void main(String[] args) {
int a = 11;
int b = 10;
// && -> 短路操作
System.out.println(a++ < 10 & b++ > 10 & a-- > 10);//false
System.out.println(a);// && 12 & 12
System.out.println(b);// && 10 & 11
}
}
5.位运算
***位运算符 -> 用二进制
&(与): 两个都是1的情况下才是1
|: 只要有一个1的情况下都是1
^(抑或): 相同为0,不同为一***
代码如下(示例):
/*
* 位运算符
*/
public class Demo04 {
public static void main(String[] args) {
//二进制
//2: 0000 0010
//4: 0000 0100
System.out.println(2 & 4);//0 -> 0000 0000
System.out.println(2 | 4);//6 -> 0000 0110
System.out.println(2 ^ 4);//6 -> 0000 0110
}
}
6.位移运算
***位移运算符 -> 二进制
>> << >>>
>>: 符号位是正数则补0,负数则补1
>>>: 符号位永远只补0, 得到的只能是正数,并且是正大数***
代码如下(示例):
/*
* 位移运算符
*/
public class Demo06 {//拿补码进行计算
public static void main(String[] args) {
System.out.println(4 >> 1);//2 相当于 ÷2
System.out.println(4 << 1);//8 相当于 *2
System.out.println(4 >>> 1);//2
//-4: 原码:1000 0100
// 反码:1111 1011
// 补码:1111 1100
System.out.println(-4 >> 1);// -2
System.out.println(4 << 1);//8 相当于 *2
System.out.println(-4 >>> 1);// 2147483646
}
}
7.三目(三元)运算符
***boolean表达式 ? 值1 : 值2
返回:true/false
如果返回true则表示 值1
如果返回false则表示 值2
作用:可以用来作为判断(if)***
代码如下(示例):
/*
* 三目运算符
*/
public class Demo07 {
public static void main(String[] args) {
int a = 20;
double d = a <= 19 ? 4.0 : 5;
System.out.println(d);//5
}
}
二、逻辑控制语句
1.分支结构
if(条件)
***条件判断 -> boolean类型
1、如果xxx,那就xxx
if(条件) -> 只要if,没有else***
<font color=#999AAA >代码如下(示例):
/*
* if()
*/
public class Demo01If {
public static void main(String[] args) {
// 如果小明考了满分,那就买个遥控汽车
int sore = 90;
// 如果小明考了满分,那就买个遥控汽车
int sore = 90;
if(sore == 100)
System.out.println("买个遥控汽车");//满足条件时,执行两条语句
System.out.println("买个电池");//不满足条件时,只执行这条语句
}
}
if(条件) - else
***如果xxxx,那就xxx,否则就xxx
if(条件) - else***
<font color=#999AAA >代码如下(示例):
/*
* if - else
*/
public class Demo02 {
public static void main(String[] args) {
// 判断一个学生成绩: 大于90分,那就打印优秀,否则就是良好
int sore = 90;
if(sore >= 90) {
//满足if条件情况下的代码
System.out.println("优秀");
}else {
//不满足
System.out.println("良好");
}
}
}
if(条件) - else if()
***如果xxx,那就xxx,否则如果xxx,那就xxxx
if(条件) - else if()
条件:
必须是boolean类型的值
true/false
比较运算符 - > boolean类型值
a > 10 / b < 10
逻辑运算符 -> boolean类型值
a > 10 && b < 10***
<font color=#999AAA >代码如下(示例):
public class Demo03 {
public static void main(String[] args) {
//如果 >= 90,标记优秀
//否则如果 >= 80,标记良好
//否则如果 >= 60,标记及格
//否则不及格
int sore = 90;
/*
if(sore >= 90) {
System.out.println("优秀");
}else {
if(sore >= 80) {
System.out.println("良好");
}else {
if(sore >= 60) {
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
}
*/
if(sore >= 90) {
System.out.println("优秀");
}else if(sore >= 80) {
System.out.println("良好");
}else if(sore >= 60) {
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
}
从控制台输入信息
import java.util.Scanner;//导包
public class Demo04 {
public static void main(String[] args) {
//JDK - Scanner类,控制台,手动输入的
Scanner console = new Scanner(System.in);//是控制台对象,用ctrl+shift+0导入包
//输入小数值
//Double d = console.nextDouble();
//输入整数
//int i = console.nextInt();
//输入字符串 - String
System.out.println("请输入");
String s1 = console.nextLine();//遇到回车就结束,这句话放在控制台下面第一行
System.out.println(s1);
System.out.println("请输入");
String s = console.next();// 遇到空格就结束
System.out.println(s);
}}
<font color=#999AAA >代码如下(示例1):
import java.util.Scanner;
/*
* 用户输入信息 : 商品单价xx 商品的数量xx
* 输入: 实付金额:1000
* 如果: 实付金额 > 500 * 0.8
* 输出: 找零金额:
*
* 如果:钱不够 -> 判断实付金额不够
*
*/
public class Demo05 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("请输入商品单价");
//商品单价
int price = console.nextInt();
System.out.println("请输入商品数量");
//商品数量
int count = console.nextInt();
System.out.println("请输入实付金额");
int money =console.nextInt();
//商品总价格
double sum = price * count;
if(sum >= 500) {
sum = sum * 0.8;
}
if(money >= sum) {
System.out.println("找零" + (money - sum) );
}else {
System.out.println("钱不够");
}
}
}
<font color=#999AAA >代码如下(示例2):
import java.util.Scanner;
/*
* 判断是不是闰年
*/
public class Demo06 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("请输入年份:");
int year = console.nextInt();
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ) { //4闰100不闰,400闰
System.out.println("是闰年");
}else {
System.out.println("不是闰年");
}
}
}
switch(条件) - case
***适用情景: 判断是否相等时使用
1.switch后跟的是要判断的内容
2.判断的内容只能放
int/byte/short/char/String
3.case具有穿透性,只有遇到break结束
4.default相当于if - else中的else***
<font color=#999AAA >代码如下(例题1):
import java.util.Scanner;
//判断每个月有多少天,输出天数
public class Demo07 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int month = console.nextInt();
/*
if(month == 1 || month == 3 || month == 5
|| month == 7 || month == 8 || month == 10
|| month == 12) {
System.out.println("天数为31天");
}else if(month == 4 || month == 6 || month == 9
|| month == 11) {
System.out.println("天数为30天");
}else if(month == 2) {
System.out.println("天数为28天");
}else {
System.out.println("不是月份");
}
*/
//switch后面跟的是要判断的month
//int/byte/short/char
//jdk 1.7 -> String
switch(month) {
//case具有穿透性的,遇到break结束
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("天数是31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("天数是30天");
break;
case 2:
System.out.println("天数是28天");
break;
default:
System.out.println("不是月份");
}
}
}
<font color=#999AAA >代码如下(例题2):
/*
- 某航空公司为吸引更多的顾客推出了优惠活动。
- 原来的飞机票价为 2500 元,
- 活动时,4~11 月旺季,头等舱 9 折,经济舱 7.5 折;
- 1~3 月、12 月淡季,头等舱 4.5 折,经济舱 4 折,求机票的价格。
* if -else
*/
public class homeWorl1 {
public static void main(String[] args) {
//1.机票原价格
int price = 2500;
Scanner console = new Scanner(System.in);
while(true) {
System.out.println("请输入查询机票的月份(退出请按0):");
int month = console.nextInt();
//退出循环的条件
if(month == 0) {
System.out.println("成功退出");
//System.exit(0);//强制退出
break;
}
System.out.println("请输入要查询的舱位:");
String change = console.next();
//2.旺季机票价格
if(month >= 4 && month <= 11) {
//4.头等舱优惠价格
if(change.equals("头等舱")) {
System.out.println("头等舱优惠价格:" + (price * 0.9));
}else if(change.equals("经济舱")) {
System.out.println("经济舱优惠价格:" + (price * 0.75));
}else {
System.out.println("输入有误,请重新输入!");
}
}
//3.淡季机票价格
else if(month >= 1 && month <=3 || month == 12) {
//头等舱优惠价格
if(change.equals("头等舱")) {
System.out.println("头等舱优惠价格:" + (price * 0.45));
}else if(change.equals("经济舱")) {
System.out.println("头等舱优惠价格:" + (price * 0.4));
}else {
System.out.println("输入有误,请重新输入!");
}
}else {
System.out.println("日期选择有误,请重新输入!");
}
}
}
}
<font color=#999AAA >代码如下(例题3):
/*
- 某航空公司为吸引更多的顾客推出了优惠活动。
- 原来的飞机票价为 2500 元,
- 活动时,4~11 月旺季,头等舱 9 折,经济舱 7.5 折;
- 1~3 月、12 月淡季,头等舱 4.5 折,经济舱 4 折,求机票的价格。
-
switch - case
*/
import java.util.Scanner;
public class homeWork2 {
public static void main(String[] args) {
//1.机票原价格
int price = 2500;
Scanner console = new Scanner(System.in);
while(true) {
//获取月份
System.out.println("请输入要出行的月份(退出请按0):");
int month = console.nextInt();
switch(month) {
case 0:
System.out.println("程序结束");
//break;
return;//返回方法,结束
//旺季的机票价格
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
System.out.println("请选择舱位:");
//int change = console.nextInt();// 1表示头等舱 ,2表示经济舱
String change = console.next();
switch(change) {
case "头等舱"://旺季头等舱优惠价格
System.out.println("头等舱优惠价格:" + (price * 0.9));
break;
case "经济舱"://旺季经济舱优惠价格
System.out.println("头等舱优惠价格:" + (price * 0.75));
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
break;
//淡季机票价格
case 1:
case 2:
case 3:
case 12:
System.out.println("请选择舱位:");
//int change = console.nextInt();// 1表示头等舱 ,2表示经济舱
change = console.next();
switch(change) {
case "头等舱":
System.out.println("头等舱优惠价格:" + (price * 0.45));
break;
case "经济舱":
System.out.println("头等舱优惠价格:" + (price * 0.4));
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
break;
default:
System.out.println("日期选择有误,请重新输入!");
break;
}
}
}
}
2.循环结构
for()
for:知道次数,或者有规律
求 1 ~ 100和
求 20!
求 3/5 ..
执行顺序:
a. int i = 1 ->进入循环的条件
b. i < 100 -> i等于这个数的时候满不满足第二个条件
c.满足则执行大括号中的代码
d.最后i++,i执行到下一个数,然后在回到第二个条件中进行判断
- For循环
/
代码如下(示例1):
/
public class Demo01For {
public static void main(String[] args) {
// 求 1 ~ 100的和
int sum = 0;
/*
* sum += 1; sum += 2; sum += 3;
*/
int i = 1;//变量的初始值
//i <= 100 -> 进入循环的条件,可以不给,不给会变成死循环
// i++ -> 步长 变化的规律
for(; i <= 100; i++) {
//for循环体
sum += i;
}
System.out.println(i);
System.out.println(sum);
/*
* for循环的执行顺序
* 1. int i = 1 -> 初始值只执行一次
* 2. i <= 100 -> 当前数字符合需求则进入循环体
* 3. 运行循环体中代码
* 4. i++
*/
}
}
/*
* 九九乘法表
*/
代码如下(示例2):
public class Demo08Work {
public static void main(String[] args) {
for(int i = 1; i < 10; i++) {
//行有几行 列就有几列
for(int j = 1; j <= i ; j++) {
System.out.print(j + "*" + i + "=" + (j*i) + "\t");
}
System.out.println();
}
}
while()
***while: 不知道次数,或者没有规律
语义环境: 当...就...***
代码如下(示例1):
/*
/*
- While循环
*/
public class Demo02While {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 100; i++) {
//for循环体
sum += i;
}
int j = 0;
while(j <= 100) {
sum += j;
j++;
}
/*
* 从 1开始叠加,叠加到多少超出500
*/
int sum1 = 0;
int z = 1;//要叠加的数,会变得
while(sum1 <= 500) {
sum1 += z;
z++;
}
System.out.println("z的次数:" + z);
System.out.println("sum的值:" + sum1);
}
}
代码如下(示例2):
/*
import java.util.Scanner;
/*
- 程序内置一个 1 到 1000 之间的数字作为猜测的结果,
- 由用户猜测此数字。 -> Scanner a
- 用户每猜测一次,由系统提示猜测结果:大了、小了或者猜对了;
-
if判断 -> num 和 a
- 直到用户猜对结果,则提示游戏结束。
-
while()
- 用户可以提前退出游戏,即,游戏过程中,如果用户录入数字0,则游戏终止。
*/
import java.util.Scanner;
public class Demo03Guess {
public static void main(String[] args) {
int num = 333;
Scanner console = new Scanner(System.in);
int a = -1;//给一个不影响程序判断的数
while(num != a) {
System.out.println("请输入:");
//1.用户猜数字
a = console.nextInt();
//1.5 提前退出游戏
if(a == 0) {
System.out.println("退出游戏");
break;
}
//2.比较
if(num > a) {
System.out.println("太小了");
}else if(num < a) {
System.out.println("太大了");
}else {
System.out.println("猜对了");
}
}
/**
* 伪代码
* 1.猜数字
* int a = "要猜的数字";
* 2.比较
* if(a > num){
* 得出结果
* }else if(a < num){
* 得出结果
* }
*/
}
}
do - while()
***do - while:先执行一次,再做循环
语义环境:做..直到..
特殊情况: 第一次不符合需求时,将不进入循环***
代码如下(示例1):
/*
* do - while
*/
public class Demo05DoWhile {
public static void main(String[] args) {
int a = 10;
//不满足条件,一次都不执行
while( a > 10) {
a--;
}
//循环之前,必须执行一次
do {
a--;
}while( a > 10);
}
}
代码如下(示例2):
/*
- 程序执行前,先猜一次
- 程序内置一个 1 到 1000 之间的数字作为猜测的结果,
- 由用户猜测此数字。 -> Scanner a
- 用户每猜测一次,由系统提示猜测结果:大了、小了或者猜对了;
-
if判断 -> num 和 a
- 直到用户猜对结果,则提示游戏结束。
-
while()
- 用户可以提前退出游戏,即,游戏过程中,如果用户录入数字0,则游戏终止。
*/
import java.util.Scanner;
public class Demo04Guess2 {
public static void main(String[] args) {
int num = 333;
Scanner console = new Scanner(System.in);
int a;
do{
System.out.println("请输入:");
//1.用户猜数字
a = console.nextInt();
//1.5 提前退出游戏
if(a == 0) {
System.out.println("退出游戏");
break;
}
//2.比较
if(num > a) {
System.out.println("太小了");
}else if(num < a) {
System.out.println("太大了");
}else {
System.out.println("猜对了");
}
}while(a != num);
/**
* 伪代码
* 1.猜数字
* int a = "要猜的数字";
* 2.比较
* if(a > num){
* 得出结果
* }else if(a < num){
* 得出结果
* }
*/
}
}
while和do-while语句的***区别***
? while循环先判断再执行;
? do-while循环先执行一次,再判断;
当初始情况不满足循环条件时,while循环一次都不会执行;
do-while循环不管任何情况都会至少执行一次。
总结
循环中的关键词
break
/*
* 循环中的关键词:break
*/
public class Demo06Loop {
public static void main(String[] args) {
int a = 0;
//循环可以嵌套
x:for(int i = 0; i < 5; i++) {
y:for(int j = 0; j < 3; j++) {
System.out.println(a++ + "--");
/*
if( j == 1) {
//结束本层循环
//外层 i -> 0 ~ 4
//内层j -> 0 和 1
break;
}
*/
if( j == 2) {
break x;
}
}
}
}
}
continue
/*
* 循环中的关键词:continue
*/
public class Demo07Lopp {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 3; j++) {
if( j == 1) {
continue;//结束本次循环,继续下一次
}
System.out.println("循环的 i = " + i +",循环的j = " + j);
}
}
}
}
/*
* 转义字符
*/
System.out.print("/t");//制表符
System.out.print("/n");//ln -> 换行
System.out.print("/r");//换行
System.out.print("//");//表示/本身
}
内容总结
以上是互联网集市为您收集整理的3、javaSE(运算符&分支&循环)全部内容,希望文章能够帮你解决3、javaSE(运算符&分支&循环)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。