一、循环高级
(1)无限循环
循环一直停不下来,如:
for(;;){
System.out.println("Study");
}
红色小方块->程序运行指示灯
注意:无限循环的下面不能再写其他代码了,会报错。
(2)跳转控制语句
1)continue
跳过本次循环,继续执行下次循环
逢7过
朋友聚会的时候可能会玩一个游戏:逢7过
游戏规则:从任意一个数字开始报数,当你要报的数字是包含7或者是7的倍数时都要说:过
需求:使用程序在控制台打印出1~100之间满足逢7过规则的数据
public class Seven{
public static void main(String[] args){
for(int i = 1;i <= 100;i++){
if( i % 7 == 0 || i % 10 == 7 || i / 10 % 10 == 7){
System.out.println("Pass");
continue;
}
System.out.println(i);
}
}
}
2)break
结束整个循环(跳出单层循环)
求平方根
键盘录入一个大于等于2的整数x,计算并返回x的平方根。
结果只保留整数部分,小数部分被舍去。
Windows快捷键:Win+R,输入calc->打开计算器
import java.util.Scanner;
public class SquareRoot{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a integer that is greater than or equal to two.");
int number = sc.nextInt();
for(int i = 1 ;i < number;i++){
if(i * i == number){
System.out.println(i + " is the suqare root of " + number + ".");
break;//提高代码效率
} else if (i * i < number && (i + 1) * (i + 1 )> number){
System.out.println(i +" is the integer part of " + number+".");
break;//提高代码效率
}
}
}
}
求质数
键盘录入一个正整数x,判断该整数是否为一个质数。
PS:如果一个整数只能被1和本身整除,那么这个数就是质数;否则是合数。
import java.util.Scanner;
public class IsPrime{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a integer to be judged.");
int number = sc.nextInt();
int i;
for( i = 2;i < number;i++){
if( number % i == 0){
System.out.println("The integer that you entered is a composite number.");
break;
}
}
if( i == number){
System.out.println("The integer that you entered is a primer number.");
}
}
}
标记思想
import java.util.Scanner;
public class IsPrime{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a integer to be judged.");
int number = sc.nextInt();
int i;
boolean flag = true;
for( i = 2;i < number;i++){
if( number % i == 0){
flag = false;
break;
}
}
if( flag){
System.out.println("The integer that you entered is a primer number.");
} else {
System.out.println("The integer that you entered is a composite number.");
}
}
}
算法简化(平方根->待写)
import java.util.Scanner;
public class IsPrime{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a integer to be judged.");
int number = sc.nextInt();
int i;
boolean flag = true;
for( i = 2;i < number;i++){
if( number % i == 0){
flag = false;
break;
}
}
if( flag){
System.out.println("The integer that you entered is a primer number.");
} else {
System.out.println("The integer that you entered is a composite number.");
}
}
}
猜数字小游戏
程序自动生成一个1~100之间的随机数字,使用程序实现猜出这个数字是多少?
import java.util.Scanner;
import java.util.Random;
public class GuessNumber{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int answer = r.nextInt(100)+1;//不能写在循环里面
System.out.println(answer);
int number;
do{
System.out.println("Please enter the number you guessed to be correct.");
number = sc.nextInt();
if(number == answer){
System.out.println("Guess Right!");
}else if (number > answer){
System.out.println("Greater!");
} else {
System.out.println("Smaller!");
}
}while(number != answer);
}
}
获取随机数
Java帮我们写好一个类叫Random,这个类就可以生成一个随机数。
①导包
import java.util.Random;
②创建对象
Random r = new Random();
③生成随机数
int number = r.nextInt(随机数的范围);//0~所写数字-1。包头不包尾,包左不包右。
获取1~100之间的随机数
import java.util.Random;
public class RandomNumber{
public static void main(String[] args){
Random r = new Random();
int number = r.nextInt(100)+1;
System.out.println(number);
}
}
求任意一个范围的随机数
1.让这个值头尾都减去一个值,让这个范围从0开始
2.尾巴+1
3.再加上减去的值
二、数组
1.数组介绍
数组指的是一种容器,可以用来存储同种数据类型的多个值(注意隐式转换)
int类型的数组容器(byte、short、int)
double类型的数组容器(byte、short、int、long、float、double)
建议:容器的类型和存储的数据类型保持一致。
2.数组的定义与静态初始化
(1)数组的定义
1)格式一(较常用)
数据类型 [] 数组名
2)格式二
数据类型 数组名 []
(2)数组的初始化
初始化:就是在内存中,为数组容器开辟空间,并将数据存入容器中的过程。
1)数组的静态初始化
完整格式
数据类型 [] 数组名 = new 数据类型 [] {元素1,元素2,元素3…};
简化格式
数据类型 [] 数组名 = {元素1,元素2,元素3…};
3.数组元素访问
1)数组的地址值
public class ArrayAddress{
public static void main(String[] args){
int [] array = {1,2,3,4};
System.out.println(array);//打印的是数组的地址值([I@776ec8df)
}
}
解释一下地址值的格式含义:
[:表示当前是一个数组
I:表示当前数组里面的元素都是int类型的
@:表示一个间隔符号(固定格式)
776ec8df:才是数组真正的地址值(十六进制)
平时我们习惯性的会把这个整体叫做数组的地址值。
2)索引
从0开始。
也叫做下标、角标。
获取数组里面的元素
数组名[索引]
把数据存储到数组当中
数组名[索引] = 具体数据/变量;
4.数组遍历
将数组中所有的内容取出来,取出来之后可以(打印、求和、判断…)
注意:遍历指的是取出数据额度过程,不要局限的理解为,遍历就是打印!
在Java当中,关于数组的一个长度属性,length。
调用方式:数组名.length
public class Erjodic{
public static void main(String[] args){
int [] array = {1,2,3,4,5};
for(int i = 0;i < array.length;i++){
System.out.println(array[i]);
}
}
}
自动的快速生成数组的遍历方式:
数组名.fori+回车
遍历数组并求和
定义一个数组,存储1,2,3,4,5
遍历数组得到每一个元素,求数组里面所有数据的和
public class ArraySum{
public static void main(String[] args){
int [] array = {1,2,3,4,5};
int sum = 0;
for(int i = 0;i < array.length;i++){
sum += array[i];
}
System.out.println("The sum of the array is " + sum);
}
}
统计个数
定义一个数组,存储1,2,3,4,5,6,7,8,9,10
遍历数组得到每一个元素,统计数组里面一共有多少个能被3整除的数字
public class CountNumber{
public static void main(String[] args){
int [] array = {1,2,3,4,5,6,7,8,9,10};
int count = 0;
for(int i = 0;i < array.length;i++){
if(array[i] % 3 == 0)
/*i:表示数组里面的每一个索引
array[i]:表示数组里面的每一个元素
*/
count++;
}
System.out.println("There are/is " + count +" number(s) that can be divided by 3.");
}
}
变化数据
定义一个数组,存储1,2,3,4,5,6,7,8,9,10
遍历数组得到每一个元素
要求:
1.如果是奇数,则将当前数字扩大两倍
2.如果是偶数,则将当前数字变成二分之一
public class ChangeArray{
public static void main(String[] args){
int [] array = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0;i < array.length;i++){
if(array[i] % 2 == 1){
array[i] *= 2;
} else {
array[i] /= 2;
}
}
System.out.println("The array after change is ");
for(int i = 0;i < array.length;i++){
System.out.print(array[i] + " ");
}
}
}
5.数组动态初始化
初始化时只指定数组长度,由系统为数组分配初始值。
(1)格式
数据类型 [] 数组名 = new 数据类型 [数组长度];
在创建的时候,由我们自己指定数组的长度,由虚拟机给出默认的初始化值。
(2)数组默认初始化值
整数类型:0
浮点数类型:0.0
字符类型:‘/u0000‘(空格)
布尔类型:false
引用数据类型(除了4类8种):null
(3)数组动态初始化与静态初始化的区别
动态初始化:手动指定数组长度,由系统给出默认初始化值。
静态初始化:手动指定数组元素,系统会根据元素个数,计算出数组的长度。
6.数组常见问题
索引越界异常(ArrayIndexOutOfBoundsExecption)
当访问了数组中不存在的索引,就会引发索引越界异常。
最小索引:0
最大索引:数组长度 - 1
7.数组常见操作
(1)求最值
已知数组元素为{33,5,22,44,55}
请找出数组中最大值并打印在控制台
public class Maximum1{
public static void main(String[] args){
int [] array = {33,5,22,44,55};
int max = array[0];
for(int i = 1;i < array.length;i++){
if(array[i] > max){
max = array[i];
}
}
System.out.println("The maximun of the array is " + max);
}
}
or
public class Maximum2{
public static void main(String[] args){
int [] array = {33,5,22,44,55};
int max = array[0];
for(int i = 1;i < array.length;i++){
max = array[i] > max ? array[i] : max;
}
System.out.println("The maximun of the array is " + max);
}
}
(2)遍历数组求和
需求:生成10个1~100之间的随机数存入数组。
1)求出所有数据的和
2)求所有数据的平均数
3)统计有多少个数据比平均值小
import java.util.Random;
public class TextDemo{
public static void main(String[] args){
Random r = new Random();
int [] array = new int [5];
int sum = 0;
double average = 0.0;
int count = 0;
for(int i = 0;i < array.length;i++){
array[i] = r.nextInt(100)+1;
}
for(int i = 0;i < array.length;i++){
sum += array[i];
}
System.out.println("The sum of the array is " + sum);
average = sum / array.length;
System.out.println("The average of the array is " + average);
for(int i = 0;i < array.length;i++){
if(array[i] < average){
count++;
}
}
System.out.println("There are/is " + count + " number(s) smaller than the average of the array.");
}
}
(3)交换数组中的数据
需求:定义一个数组,存入1,2,3,4,5.按照要求交换索引对应的元素。
交换前:1,2,3,4,5
交换后:5,4,3,2,1
public class Exchange{
public static void main(String[] args){
int [] array = {1,2,3,4,5};
for(int i = 0,j = array.length - 1;i < j;i++,j--){
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
System.out.println("The array after exchange is ");
for(int i = 0;i < array.length;i++){
System.out.print(array[i] + " ");//不换行打印
}
}
}
(4)打乱数组中的数据
定义一个数组,存入1~5.要求打乱数组中所有数据的顺序。(随机索引)
import java.util.Random;
public class DisruptArray{
public static void main(String[] args){
Random r = new Random();
int [] array = {1,2,3,4,5};
for(int i = 0;i <array.length;i++){
int j = r.nextInt(array.length);//具有通用性:获取数组中的随机索引
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
System.out.println("The array after disrupt is ");
for(int i = 0;i< array.length;i++){
System.out.print(array[i] + " ");
}
}
}
8.数组内存图
JDK7以前,方法区跟堆是连在一起的,在物理内存中是一块连续的空间(不好)。
JDK8开始,取消方法区,新增元空间。把原来方法区的多种功能进行拆分,有的功能放到了堆中,有的功能放到了元空间中。
*(1)栈
方法运行时使用的内存,比如main方法运行,进入方法栈中执行,运行完毕就出栈。
变量里面存储的是数值
在栈中,数组(名)里面存储的是数组在堆中的首地址
*(2)堆
存储对象或者数组,new来创建,都存储在堆空间
数组的静态初始化的简化格式其实是省略了,还是有new关键字,使用在堆中开辟空间存储数组的值,有地址值存储在栈中数组(名)中
先通过数组名找到一个空间,再通过索引找到数组中所存储的对应的值
两个数组指向同一个空间
int [] arr1 = {11,22};//在堆中开辟一段空间,存储数组中的值,并将改空间的地址值存到栈中数组arr1中
int [] arr2 = arr1;//因为没有使用new关键字,不会在堆中开辟空间存储arr2数组中的值;但是会在栈中开辟一个空间存储arr2的地址值(也是arr1的地址值)
arr2[0] = 33;//因为arr2中存的值和arr1中存的值指向的是同一片内存,所以改动数组中0索引的值,共同指向的那个数组中的值发生改变
System.out.println(arr1[0]);//访问arr1中0索引的值,为改变之后的值
(3)方法区
存储可以运行的class文件
(4)本地方法栈
JVM在使用操作系统功能的时候使用,和我们开发无关
(5)寄存器
给CPU使用,和我们开发无关