运算符
逻辑运算符
& 逻辑与:都为真,才为真 && 短路与:如果左边是false,则右边不执行
| 逻辑或:一个为真 则为真 || 短路或:如果左边是true,则右边不执行
^ 异或:一样为false,不一样为true ! 非 :取反
三目运算符
X?Y:Z 如果X成立,则执行Y,反之执行Z
三目运算符:必须有变量接受结果, 效率更高
速度快, 必须有结果
// 如果 80 则 优秀 良好
int score = 59;//
String str = score>80 ? "优秀":(score>=60?"良好":"不及格");
System.out.println(str);
if、switch
有一些需求 没有结果只是想要执行特定代码, 需要用分支
顺序结构:
从上到下一条一条执行
选择结构 :
有选择性的执行或者跳过某段语句
if分支
switch分支
循环结构:
反复执行某段特定的代码
单分支
if(){
}
双分支
if(){
}else{}
分支嵌套
多分支结构
if(){
}else if(){
}else{}
如果大括号中只有一条语句,则大括号可以省略
if语句
String str2= "吃了";
if(str2=="吃了"){
System.out.println("吃了");
}
String str3= "5分饱";
if(str3=="吃了"){
System.out.println("吃了");
}else{
System.out.println("没吃");
}
if(str3 =="没吃"){
System.out.println("没吃");
}else {
if(str3=="3分饱"){
System.out.println("3分饱");
}else{
if(str3=="5分饱"){
System.out.println("5分饱");
}else{
System.out.println("吃");
}
}
}
// if else if
if(str3=="没吃"){
System.out.println("没吃");
}else if(str3=="3分饱"){
System.out.println("3分饱");
}else if(str3=="5分饱"){
System.out.println("5分饱");
}else{
System.out.println("吃");
}
== 判断的是基本数据类型的数值是否相同 , 判断引用数据类型的内存地址是否相等
String类中的 equals 比较的是 两个字符串的常量池中的内容是否相同
if(season.equals("春")){
System.out.println("万物复苏");
}else if(season.equals("夏")){
System.out.println("坐空调屋里吃火锅");
}else if(season.equals("秋")){
System.out.println("秋天的蚊子比较大");
}else{
System.out.println("坐在炕上吃老冰棍");
}
// 范围
int age = 18;
if(age>=18){
System.out.println("成年人");
}else{
System.out.println("小年轻");
}
switch语句
switch 中 可以使 byte short int char String(jdk1.7) 枚举
switch 专门用来做等值判断的
switch(变量){
case 值:
break;
case 值:
break;
case 值:
break;
default:
break;
}
Scanner sc = new Scanner(System.in);
System.out.println("请输入季节:");
String season = sc.next();
switch(season){
case "春":
System.out.println("万物复苏");
break;
case "夏":
System.out.println("坐空调屋里吃火锅");
break;
case "秋":
System.out.println("秋天的蚊子比较大");
break;
case "冬":
System.out.println("坐在炕上吃老冰棍");
break;
default:
System.out.println("外星人");
break;
}
2000 的2月份的天数
switch(month){
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;
}
闰年
// 能整除4 但是不能整除100 , 能整除400 的都是闰年
Scanner sc2 = new Scanner(System.in);
System.out.println("请输入年份");
int num = sc2.nextInt();
System.out.println("请输入月份:");
int month2 = sc2.nextInt();
if((num%4==0 && num%100!=0) || num%400==0){
// 闰年
if(month2==2){
System.out.println("29天");
}
}else{
if(month2==2){
System.out.println("28天");
}
}
switch(month2){
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;
}
总结(三目、if、switch)
三目运算符 : 速度快, 必须有结果
if 分支: 既可以判断范围,也可以等值判断
switch : 只能做等值判断
如果有结果, 选择三目运算符
如果只做等值等值判断 switch
如果范围判断 if
循环: 反复执行特定的代码
循环
while、 do-while、 for
正常用: for
至少执行一次: do-while
不确定循环次数: while(true){break;}
do-while 至少能执行一次
while 则一次也不执行
while
// 1 定义循环变量 记录循环次数
int i= 1;
//2. 循环条件
while(i<=10){
//3. 反复要被执行的内容
System.out.println("跑"+i+"圈");// 1 2 3
System.out.println(i);
// 4. 迭代部分 , 更新循环变量
i++;
}
int j = 2;
while(j<=10){
System.out.println(j);
j+=2;
}
int sum = 0;
int k =2;
while(k<=10){
sum+=k;
// 2 4 6 8 10
k+=2;
}
System.out.println("sum="+sum);
不确定循环次数 如果年龄是30,则退出循环
while(true){
int age = sc.nextInt();
if(age==30){
break;
}
}
do-while
循环变量,先执行do,然后再执行while
int i = 1;
do{
// 循环体
System.out.println("跑一圈");
// 迭代部分
i++;
}while(i<1);// 循环条件
int j = 1;
do{
System.out.println(j);
j+=2;
}while(j<=10);
for
for效率最高
for (int i = 1; i <=10; i++) {
System.out.println("跑"+i+"圈");
}
不确定循环次数,循环输入年龄,如果年龄是30,则退出循环
for(;;){
int age = sc.nextInt();
if(age==30){
break;
}
}
二维图形for循环
二维图形:外层循环控制行,内层循环控制列
for (int i = 0; i <5; i++) {
for (int j = 0; j < 6; j++) {
System.out.print("*");
}
System.out.println();
}
/* * i=1 j<=1 k = 3 k<=4-i;
* *** i=2 j<=3 k = 2 k<=4-2
* ***** i=3 j<=5 k = 1 k<=4-3
* ******* i=4 j<=7 k = 0 k<=4-4
*
* * i=1 j<=1
* ** i=2 j<=2
* *** i=3 j<=3
* ****
*
*/
结果:
for (int i = 1; i <= 4; i++) {
// 内层
for(int j = 1; j<=i ;j++){
System.out.print("*");
}
System.out.println();
}
结果:
for (int i = 1; i <= 4; i++) {
for(int k = 1;k<=4-i;k++){
System.out.print(" ");
}
for (int j = 1; j <=2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
结果:
九九乘法表
for (int i = 1; i <=9; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println();
}
结果:
break、return、continue
break 默认跳出的 就近原则
return 结束方法
continue 跳过本次循环, 执行下一次循环
前面加一个out ,break out;跳出的是 jvm System.exit(0);
out:for (int i = 0; i < 3; i++) {
in: for(int j = 0;j<2;j++){
if(j==1){
// 跳出的是 jvm System.exit(0);
//return;// 结束方法
break out;
}
System.out.println(i+"-->"+j);
}
}
// continue 跳过本次循环, 执行下一次循环
for (int i = 0; i < 5; i++) {
if(i==3){
continue;
}
System.out.println(i);
}
数组
数组: 小书包 同时 存储 和管理 多个数据
创建数组
int[] 数组类型一旦确定,则只能存储该类型或者是 可以自动类型转换成该类型的数据
int[] arr = new int[3];
double[] d = new double[3];
d[0] = 3.3;
byte[] b = new byte[3];
b[0] = 1;
char[] cs = new char[3];
cs[0] = 'a';
boolean[] b2 = new boolean[3];
b2[0] = true;
String[] str = new String[3];
str[0] = "你好";
for (String string : str) {
System.out.println(string);
}
// 可以实现带有下标的操作
for (int i = 0; i < arr3.length; i++) {
System.out.println(arr3[i]);
}
遍历数组 集合 用增强for循环 效率更高
for (int i : arr3) {
System.out.println(i);
}
ArrayIndexOutOfBoundsException 数组下标越界
// 数组的长度是4
int[] arr = new int[4];
arr[0] = 5;
arr[1] = 6;
arr[2] = 7;
arr[3] = 8;
// ArrayIndexOutOfBoundsException
arr[2] = 6;
在声明初始化数组的时候 就知道 要存什么
int[] arr2 = new int[]{1,2,3,4};
int[] arr3 = {11,12,13,14,15};
arr3.lenth 数组长度 涉及带有下标的操作 , 用普通for循环
for (int i = 0; i < arr3.length; i++) {
System.out.println(i);
}
增强for循环
i 其实数组中元素的内容
* 格式简洁, 效率更高
* 不能拿到下标的
* 只适用于遍历操作
for (int num : arr3) {
System.out.println(num);
}
例题
所有数的加和
int[] arr = {6,3,8,66,344,1 };
//1.所以数的加和
int sum = 0;
for (int num : arr) {
sum+=num;
}
System.out.println(sum);
最大值和最小值
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if(max<arr[i]){
max= arr[i];
}
}
System.out.println("最大值:"+max);
判断是否有某一个元素:
顺序查找
Scanner sc= new Scanner(System.in);
System.out.println("请输入想要查找的值");
int num = sc.nextInt();
boolean f = false;//
for (int i = 0; i < arr.length; i++) {
if(num==arr[i]){
f = true;
break;
}
}
if(f){// f==true
System.out.println("存在");
}else{
System.out.println("不存在");
}
二分查找 前提条件:数组是有顺序的
int[] arr2 = {1,5,8,9,12,15};
// 15
int index = binarySerach(arr2,9);
System.out.println(index);
int index2 = Arrays.binarySearch(arr2, 9);
System.out.println(index2);
public static int binarySerach(int[] arr , int key){
// 最小值:
int minIndex = 0;
int maxIndex = arr.length-1;
while(minIndex<=maxIndex){
int middleIndex = (minIndex+maxIndex)/2;
// 15 > 8
if(key > arr[middleIndex]){
minIndex = middleIndex+1;
// 5 < 8
}else if(key <arr[middleIndex]){
maxIndex = middleIndex-1;
// 8 8
}else{
// 找到了
return middleIndex;
}
}
return -1;
}
冒泡排序
for (int i = 0; i < arr3.length-1; i++) {
// 0 1 2 3
// 4 3 2 1 4-i
for (int j = 0; j < arr3.length-1-i; j++) {
if(arr3[j]>arr3[j+1]){
int temp = arr3[j];
arr3[j] = arr3[j+1];
arr3[j+1] = temp;
}
}
}
sort函数排序
底层利用的是快速排序
int[] arr = new int[]{4,2,8,7,9,1};
// sort 函数 进行排序
Arrays.sort(arr);
// 将数组中元素 拼接成一个 字符串 用来展示数组中元素内容
String str = Arrays.toString(arr);
System.out.println(str);
二维数组
创建二维数组
// 更加推荐第一种写法
int[][] arr ;
int[] arr2[];
int arr3[][];
// 数组在声明的时候 就必须 确定长度
arr = new int[2][3];// 2个一维数组 每个一维数组的长度为 3
arr[0][0] = 5;
arr[0][1] = 5;
arr[0][2] = 5;
arr[1][2] = 8;
arr[1][1] = 8;
arr[1][0] = 8;
System.out.println(arr[0][0]);
System.out.println(arr[1][2]);
arr3 = new int[3][];
// null.aaaa = NullPoniterException
// 没有初始化 就调用 会造成空指针异常
// 所以要先初始化
arr3[0] = new int[5];
arr3[1] = new int[3];
arr3[2] = new int[4];
/*int[][] arr = new int[3][];
只初始化了二维数组, 一维数组并没有初始化, 如果想要使用一维数组
*必须通过 arr[0] = new int[2]; 方式给每一个一维数组进行初始化
*每个一维数组的长度可以不同
*int[][] arr = new int[3][4];
*二维数组一维数组全部进行了初始化
*每一个一维数组长度是相同的*/
int[][] arr2 = new int[][]{{1,2,3},{4,5},{6,},{7,8}};
int[][] arr3 = {{2,3},{6}};
遍历二维数组
//arr3.length 代表的是 二维数组中有几个一维数组
for (int i = 0; i < arr3.length; i++) {
System.out.println(Arrays.toString(arr3[i]));
}
for (int i = 0; i < arr3.length; i++) {
for (int j = 0; j < arr3[i].length; j++) {
System.out.print(arr3[i][j]);
}
System.out.println();
}
二维数组例题
需求: 存储 小明 小红 小兰 小绿 语文成绩 英语成绩 数学成绩 物理成绩
控制台输入, 要求计算每个同学的总成绩
int[][] stus = new int[4][4];
Scanner sc = new Scanner(System.in);
// 外层循环控制一维数组的个数
for (int i = 0; i < stus.length; i++) {
for (int j = 0; j < stus[i].length; j++) {
System.out.println("请输入第"+(i+1)+"个同学的第"+(j+1)+"的成绩");
stus[i][j] = sc.nextInt();
}
}
// 总成绩
for (int i = 0; i < stus.length; i++) {
int sum = 0;
for (int j = 0; j < stus[i].length; j++) {
sum+=stus[i][j];
}
System.out.println("第"+(i+1)+"个同学的总成绩:"+sum);
}
可变参数
可变参数 实质上是一个 可变长度数组 , 可变长度数组
自己重新定义一个新的更长的数组, 把原来的数据存过来,
一个方法至多只能有一个可变参数, 并且可变参数必须放在最后一个位置
public static void b(String name,String...sex){
}
public static void main(String[] args) {
a("aa",2,"a","b","d","f");
// 可变参数传值的个数 从 0~n
b("aa","b","d");
}
public static void a(String namem,int age,String... stu){
System.out.println(namem+" "+age);
for (int i = 0; i < stu.length; i++) {
System.out.println(stu[i]);
}
}